Blakester Blakester - 10 days ago 5
Python Question

Iterating over a large csv file

I'm a new programmer and well here's what I'm trying to accomplish. So the variables word1 and word2 in my function are inputs from the user.

What I need this function to do for me is to iterate through this csv file to see if their word is in fact in the file( located in column 1).If it is in the file, then I need to gather the year which is located in column 2 and how many times the word was used which is found in column 3 of the file.

The problem is, I'm not exactly sure how to do that and below is my code so far although I know it doesn't work properly. Lastly it's a huge file and I'm not sure since this is the case, if I need to do anything differently.

Looking for suggestions and some example code if possible, thank you so much in advance!

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

Answer
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