I'm doing a word count of a source code, and for example, I want to know how many for there are in a txt, for now it does well, but in some cases programmers write this way: for( or for (. In my case my code only counts the for ( with the space but not without the space, how could I solve this?. Also in some cases, programmers put for example, for(xxx or for (xxx or for ( xxx, how could I make only for ?
from collections import Counter
words_to_keep = {"for", "setup()", "loop()"}
def word_count(filename):
with open('hello.txt', 'r') as f: # use `filename`
return Counter(w for w in f.read().split() if w in words_to_keep)
counter = word_count('hola.txt')
for i in counter:
print (i, ":", counter [i])