-1

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

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

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

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

4

1 回答 1

0

You missed a :

if line in analysis == x:
                        ^
                        |

Try something like this:

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
于 2013-07-04T11:10:35.017 回答