2016-11-27 64 views
0

我是一个新的程序员,以及这是我想要完成的。所以我的函数中的变量word1和word2是来自用户的输入。迭代一个大的csv文件

我需要这个函数做的是我遍历这个csv文件,看他们的单词是否在文件中(实际位于第1列)。如果它在文件中,那么我需要收集位于第2列的年份以及该文件第3列中使用该词的次数。

问题是,我不完全确定如何做到这一点,以下是我的代码,虽然我知道它不能正常工作。最后这是一个巨大的文件,我不确定,因为这是事实,如果我需要做不同的事情。

寻找建议和一些示例代码如果可能,非常感谢你提前!

def compare(word1, word2, startDate, endDate): 
    with open('all_words.csv') as allWords: 
     readWords = csv.reader(allWords, delimiter=',') 
     for word1 in readWords: 
      print(row) 
+0

你需要的单词与在列1的内容精确匹配? – timrau

+0

是的,它应该区分大小写。 – Blakester

回答

0
def compare(word1, word2, startDate, endDate): 
    with open('all_words.csv') as allWords: 
     readWords = csv.reader(allWords, delimiter=',') 
     for row in readWords: # row, not word1 
      if row[0] == word1: 
       # Here you have one of the desired lines. Read row[1], row[2], etc. 
       pass