I am trying to count the keywords in a .py
file but the code I wrote is also counting keywords which occur in strings.
How can I differentiate between actual keywords and the ones in strings? For example: is
, with
and in
are keywords, but you can also spot those in comments and user input strings. This is what I have tried:
from collections import Counter
import keyword
count = {}
scode = input("Enter the name of Python source code file name :")
with open(scode,'r') as f:
for line in f:
words = line.split()
for i in words:
if(keyword.iskeyword(i)):
count[i]= count.get(i,0)+1
print(count)