如何计算代码中关键字的出现次数而忽略注释/docstring中的关键字?

2024-03-28 19:18:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python很陌生。我想在下面的代码中找到Python关键字['def','in', 'if'...]的出现。但是,需要忽略代码中任何字符串常量中的关键字。 在不计算字符串中的关键字的情况下,如何计算关键字的出现次数?在

def grade(result):
    '''
    if if (<--- example to test if the word "if" will be ignored in the counts)
    :param result: none
    :return:none
    '''

    if result >= 80:
        grade = "HD"
    elif 70 <= result:
        grade = "DI"
    elif 60 <= result:
        grade = "CR"
    elif 50 <= result:
        grade = "PA"
    else:
    #else (ignore this word)
        grade = "NN"
    return grade

result = float(raw_input("Enter a final result: "))

while result < 0 or result > 100:
    print "Invalid result. Result must be between 0 and 100."
    result = float(raw_input("Re-enter final result: "))

print "The corresponding grade is", grade(result)

Tags: the字符串代码innonereturnifdef
1条回答
网友
1楼 · 发布于 2024-03-28 19:18:47

使用tokenizekeyword和{}模块。在

tokenize.generate_tokens(readline)

The generate_tokens() generator requires one argument, readline, which must be a callable object which provides the same interface as the readline() method of built-in file objects (see section File Objects). Each call to the function should return one line of input as a string. Alternately, readline may be a callable object that signals completion by raising StopIteration.

The generator produces 5-tuples with these members: the token type; the token string; a 2-tuple (srow, scol) of ints specifying the row and column where the token begins in the source; a 2-tuple (erow, ecol) of ints specifying the row and column where the token ends in the source; and the line on which the token was found. The line passed (the last tuple item) is the logical line; continuation lines are included.

New in version 2.2.

import tokenize
with open('source.py') as f:
    print list(tokenize.generate_tokens(f.readline))

部分输出:

^{pr2}$

您可以从keyword模块检索关键字列表:

^{3}$

集成解决方案集合。计数器公司名称:

import tokenize
import keyword
import collections 
with open('source.py') as f:
    # tokens is lazy generator
    tokens = (token for _, token, _, _, _ in tokenize.generate_tokens(f.readline))
    c = collections.Counter(token for token in tokens if keyword.iskeyword(token))

print c  # Counter({'elif': 3, 'print': 2, 'return': 1, 'else': 1, 'while': 1, 'or': 1, 'def': 1, 'if': 1})

相关问题 更多 >