简体   繁体   中英

python count keywords in a python file without counting inside quotation marks

For example:

import codecs

def main():
    fileName = input("Please input a python file: ")
    file = codecs.open(fileName, encoding = "utf8")
    fornum = 0
    for line in file:
        data = line.split()
        if "for" in data:
            fornum += 1
    print("The number of for loop in", fileName, ":", fornum)

main()

There are 1 for-statement in above codes. But the program counts the 'for' inside the quotation mark which is not expected and it displays 2. How can I change the codes to make it counts the keywords(for) without counting the words inside ""? Thx

As mentioned in comments to propely count for loops you should parse Python file and walk through it AST. You could do it with ast module. Example code:

import ast

def main():
    fileName = input("Please input a python file: ")
    with open(fileName) as f:
        src = f.read()
        source_tree = ast.parse(src) # get AST of source file
    fornum = 0
    # and recursively walk through all AST nodes
    for n in ast.walk(source_tree):
        if n.__class__.__name__ == "For":
            fornum = fornum+1
    print("The number of for loop in ", fileName, ":", fornum)

main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM