Monday, 26 August 2013

How to find a string in a text file, Python

How to find a string in a text file, Python

How do I get the number of times a certain 2 characters are used in a text
files, (e.g. ('hi')) And how do I print the sum out as an int?
I tried doing this:
for line in open('test.txt'):
ly = line.split()
for i in ly:
a = i.count('ly')
print(sum(a))
But it failed, thanks in advance!

No comments:

Post a Comment