识别和计算python中的文本

时间:2013-07-04 11:09:05

标签: python dictionary

我有一个制表符分隔文件,我正在尝试从中提取特定信息。基本上,我想搜索每一行,找到一个标识符,然后计算文本中出现的次数。我想遍历文本并将信息存储在字典中。

这是我到目前为止所拥有的:

c_count = {"c1":0, "c2":0, "c3":0, "c4":0, "c5":0}

analysis = open("myInputFileName")

for x in c_count:
    if line in analysis == x
        c_count[x] = c_count[x] + 1

        print c_count

我收到错误:

    if line in analysis == x
                           ^
SyntaxError: invalid syntax

我做错了什么..? 谢谢!

1 个答案:

答案 0 :(得分:0)

您错过了:

if line in analysis == x:
                        ^
                        |

尝试这样的事情:

c_count = {"c1":0, "c2":0, "c3":0, "c4":0, "c5":0}
with open("myInputFileName") as analysis:
    for line in analysis:            #read lines one by one
       line = line.strip()           #strip white-spaces
       if line in c_count:           #if line is found in c_count, increase it's count
          c_count[line] += 1