python count keywords in a python file without counting inside quotation marks
Question: 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


Answer: 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](https://docs.python.org/2/library/ast.html) 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()
    

