Python 如何计算代码中关键字的出现次数,但忽略注释/文档字符串中的关键字?
我对Python很陌生。我想在下面的代码中找到Python关键字Python 如何计算代码中关键字的出现次数,但忽略注释/文档字符串中的关键字?,python,file,python-2.7,count,keyword,Python,File,Python 2.7,Count,Keyword,我对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
['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)
def等级(结果):
'''
如果(使用标记化
,关键字
和集合
模块
tokenize.生成令牌(readline)
generate_tokens()生成器
需要一个参数readline,该参数必须是
提供与内置文件的readline()方法相同的接口
对象(请参见“文件对象”一节)。对函数的每次调用
以字符串形式返回一行输入。或者,readline可以是
通过引发StopIteration发出完成信号的可调用对象
生成器生成包含以下成员的5元组:令牌类型;
标记字符串;指定行的整数的2元组(srow,scol)
以及标记在源中开始的列;2元组(erow,
指定标记在其中结束的行和列的int的ecol)
源;以及在其上找到令牌的行。该行已通过
(最后一个元组项)是逻辑行;连续行是
包括在内
版本2.2中的新功能
部分输出:
[(1, 'def', (1, 0), (1, 3), 'def grade(result):\n'),
(1, 'grade', (1, 4), (1, 9), 'def grade(result):\n'),
(51, '(', (1, 9), (1, 10), 'def grade(result):\n'),
(1, 'result', (1, 10), (1, 16), 'def grade(result):\n'),
(51, ')', (1, 16), (1, 17), 'def grade(result):\n'),
(51, ':', (1, 17), (1, 18), 'def grade(result):\n'),
(4, '\n', (1, 18), (1, 19), 'def grade(result):\n'),
(5, ' ', (2, 0), (2, 4), " '''\n"),
(3,
'\'\'\'\n if if (<--- example to test if the word "if" will be ignored in the counts)\n :param result: none\n :return:none\n \'\'\'',
(2, 4),
(6, 7),
' \'\'\'\n if if (<--- example to test if the word "if" will be ignored in the counts)\n :param result: none\n :return:none\n \'\'\'\n'),
(4, '\n', (6, 7), (6, 8), " '''\n"),
(54, '\n', (7, 0), (7, 1), '\n'),
(1, 'if', (8, 4), (8, 6), ' if result >= 80:\n'),
具有集合的集成解决方案。计数器:
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})
哇,我很抱歉两年前没有接受你的答案。我在第一次上编程课时问了这个问题,当时我无法理解和实现你的代码。非常感谢你的回答!
import keyword
print keyword.kwlist
print keyword.iskeyword('def')
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})