I'm trying to open and read multiple csv files. My goal is to get values from different columns/rows located in different csv's and put it into lists that I will use later to display in a table. I have been looking through online and found some information but I'm still having trouble getting it to work. I am receiving an error: NameError:name 'row' is not defined
.
I am relatively new to python so I don't have to much experience with a lot of modules yet, so could you also let me know if there is a better way to attack this problem. Also, the directory I am using has other files inside besides just .csv's.
import os
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')
if row['rankings'] == 6:
testrank.append(row['Name'])
f.close()
print(testrank)
The variable row
has indeed not been set, so it cannot be used.
You first need to "read" the file f
; create a "reader" where you can iterate among rows:
...
import csv
...
if file.endswith(".csv"):
f=open(file, 'r')
myfilereader = csv.reader(f, delimiter=' ', quotechar='|')
for row in myfilereader:
if row['rankings'] == 6:
...