Searching through a file in Python
<p>Say that I have a file of restaurant names and that I need to search through said file and find a particular string like "Italian". How would the code look if I searched the file for the string and print out the number of restaurants with the same string?</p>

<pre><code>f = open("/home/ubuntu/ipynb/NYU_Notes/2-Introduction_to_Python/data/restaurant-names.txt", "r")
content = f.read()
f.close()
lines = content.split("\n")
with open("/home/ubuntu/ipynb/NYU_Notes/2-Introduction_to_Python/data/restaurant-names.txt") as f:
      print ("There are", len(f.readlines()), "restaurants in the dataset")
with open("/home/ubuntu/ipynb/NYU_Notes/2-Introduction_to_Python/data/restaurant-names.txt") as f:
        searchlines = f.readlines()
    for i, line in enumerate(searchlines):
    if "GREEK" in line: 
        for l in searchlines[i:i+3]: print (l),
        print
</code></pre>

<p>You input the file as a string.<br/>
Then use the count method of strings.<br/>
Code:</p>

<pre><code>#Let the file be taken as a string in s1
print s1.count("italian")
</code></pre>

python file