2016-11-27 89 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 
相關問題