简体   繁体   English

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

[英]Unexpected output while reading a csv file in a for loop

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"]

Results: 结果:

0.75
0.0 
124567

The results above are only for the 1st word, ie "great". 上面的结果仅适用于第一个单词,即“伟大”。 The loop ends here - it doesn't go on to the next word. 循环到此结束-不会继续到下一个单词。

Once you iterate through the rows in reader , it won't magically restart. 一旦您遍历了reader的行,就不会神奇地重新启动。 Swap the for loops so that you iterate only once through reader : 交换for循环,以便您仅通过reader迭代一次:

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

I'd just avoid iterating twice by just checking if each word is in a set of words you're interested in. It'll run faster: 我只是通过检查每个单词是否在您感兴趣的一组单词中来避免重复两次。它将运行得更快:

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"]

You may also want to consider using a package like pandas for working with tables, it usually makes your life easier. 您可能还需要考虑使用像pandas这样的程序包来处理表格,这通常会使您的生活更轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM