需要帮助只计算某些单词(Python)

时间:2013-05-28 03:29:22

标签: if-statement for-loop python-3.x count

到目前为止,我已经编写了这段代码:

"""test""" 
from collections import Counter
FOR_ = 0
WHILE_ = 0
SAUCE_ = None
IF_ = 0
DEF_ = 0
INPUT_ = input("Enter file or directory: ")
print ("Enter file or directory: {0}".format(INPUT_))
print ("Filename {0:>20}".format(INPUT_))
BEE_ = open(INPUT_)
CEE_ = BEE_.readlines()
CAT_ = (str(CEE_))
FAT_ = CAT_.strip()
with open(INPUT_) as EFF_:
    i = 1
    for SAUCE_ in EFF_:
        if i == (int(2)):
            break
        i += 1
    DEE_ = list((SAUCE_))
NNN_ = (DEE_[8:-4])
GGG_ = ''.join(NNN_)
print ("Author {0:>29}".format(GGG_))
if len(CEE_)<=9:
    print ("lines_of_code {0:>8}".format((len (CEE_))))
elif len(CEE_)>=10:
    print ("lines_of_code {0:>9}".format((len (CEE_))))
COUNTER_ = Counter(str(CEE_))
for LINE_ in CEE_:
    if  ("for ") in LINE_:
        FOR_ += 1
for LINE_ in CEE_:
    if  ("while ") in LINE_:
        WHILE_ += 1
for LINE_ in CEE_:
    if  ("def if ") in LINE_:
        IF_ += 0
        AYE_ = IF_
    elif ("if ") in LINE_:
        IF_ += 1
        AYE_ = IF_
for LINE_ in CEE_:
    if  ("def ") in LINE_:
        DEF_ += 1
print ("for_loops {0:>12}".format((FOR_)))
print ("while_loops {0:>10}".format((WHILE_)))
print ("if_statements {0:>8}".format((AYE_)))
print ("function_definitions {0}".format((DEF_)))
print ("multiplications {0:>6}".format((COUNTER_['*']))) 
print ("divisions {0:>12}".format((COUNTER_['/'])))
print ("additions {0:>12}".format((COUNTER_['+'])))
print ("subtractions {0:>9}".format((COUNTER_['-'])))

到目前为止,它计算了每个单词的实例:for,def,if和while。然而,我想要它只是计算这些单词中的每一个,如果它们之前没有任何这些单词。例如:

for def if

只会算作。

另外,我希望文件中的任何内容后跟哈希值都被忽略:

#for you

不会算作for。

非常感谢任何帮助。

0 个答案:

没有答案