在for循环中读取csv文件时出现意外输出

时间:2016-04-28 19:01:27

标签: python csv sentiment-analysis

import csv
if __name__ == "__main__":
    words = ["great" , "thanks"]
    with open("data/sentiwordnet.tsv", "r") as f:
        reader = csv.DictReader(f,delimiter='\t')
        for word in xrange(len(words)):
             for row in reader:
                 if row['word_en'] == words[word]:
                    print float(row["positive"])
                    print float(row["negative"])
                    print row["synset"]

结果:

0.75
0.0 
124567

以上结果仅适用于第一个单词,即“很棒”。循环在这里结束 - 它不会继续下一个单词。

1 个答案:

答案 0 :(得分:5)

一旦遍历reader中的行,它就不会重新启动。交换for循环,以便只通过reader迭代一次:

for row in reader:
    for word in xrange(len(words)):

我只是通过检查每个单词是否在您感兴趣的一组单词中来避免迭代两次。它会跑得更快:

import csv

if __name__ == "__main__":
    words = {"great" , "thanks"}  # sets are faster than lists for checking containment

    with open("data/sentiwordnet.tsv", "r") as f:
        reader = csv.DictReader(f, delimiter='\t')

        for row in reader:
            if row['word_en'] in words:
                print float(row["positive"])
                print float(row["negative"])
                print row["synset"]

您可能还想考虑使用像pandas这样的程序包来处理表格,这通常会让您的生活更轻松。