[Solved] python 3 Acess file and check number of time phrase comes up [closed]


Assuming that the count result applies to the whole file you can use a collections.Counter:

from collections import Counter

with open('input.tsv') as infile:
    counts = Counter(infile.read())

for c in 'SF':
    print '{}: {}'.format(c, counts.get(c))

This has the advantage of allowing you to obtain counts of any character (not just “S” and “F”) with one pass of the file.

You could also just use str.count() for a specific character (or a string), but if you need counts more than one character you’ll find a Counter more convenient and probably faster too.

solved python 3 Acess file and check number of time phrase comes up [closed]