1

I'm trying to retrieve values from multiple csv files that are located in a directory. When performing if row['opponent-points-per-game-rank'] == 6: I am getting a Key Error. When I do something like:

if 'opponent-points-per-game-rank' in myfilereader.fieldnames:
    testrank = [row.get('opponent-points-per-game-rank') for row in myfilereader]

It will find the column name. Any reason why it can find the column name this way but not the other? Here is the link to my csv file. My code right now:

import os
import csv

testrank = []

 
directory = os.path.join("c:\\","Users\sm\OneDrive\TestProject") 

for root,dirs,files in os.walk(directory):
    for file in files:
       if file.endswith(".csv"):
           f=open(file, 'r')
           myfilereader = csv.DictReader(f)
           for row in myfilereader:
               if row['opponent-points-per-game-rank'] == 6:
                  testrank.append(row['Team'])
           f.close() 

print(testrank)

2 Answers 2

2

I figure that row is an OrderedDict object. Here is my solution. Notice that you need to cast v to int because it is str.

import os
import csv

testrank = []

 
directory = os.path.join("c:\\","Users\sm\OneDrive\TestProject") 

for root,dirs,files in os.walk(directory):
    for file in files:
       if file.endswith(".csv"):
           f=open(file, 'r')
           myfilereader = csv.DictReader(f)
           for row in myfilereader:
               for k,v in row.items():
               if k == 'opponent-points-per-game-rank' and int(v) == 6:
                  testrank.append(row['Team'])
           f.close() 

print(testrank)
Sign up to request clarification or add additional context in comments.

Comments

2

I tried to reproduce this issue using the file you linked, but I am not getting any errors. However, the value of row['opponent-points-per-game-rank'] is a string, so row['opponent-points-per-game-rank'] == 6 always evaluates to False. When this condition is replaced by int(row['opponent-points-per-game-rank']) == 6 everything seems to work fine.

2 Comments

I concur. I was going to say the same thing. I get a line for each number from 1-357, and then a line with [] if I don't put quotes around the 6, and ['Princeton'] if I do. - you're potentially reading other files that we don't have since you do a search for any file with a .csv extension. Could it be that you are reading a different file that doesn't have that column in it?
Ok that might be the reason because even after I added the int(), I was still getting the KeyError. However when I applied it to that single file it worked. In theory though, wouldn't this script go down the list of each csv file and search for the the parameter in each column heading? @cryptoFool and bb1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.