Reputation: 11
Hi i'm trying to pass all rows which has index 7 = "Danmark" to another file. From a CSV file, i get the error "IndexError: list index out of range". Hope you guys can help me out.
import csv #import the module csv
with open('akassedatareduced.csv', encoding="ISO-8859-1") as csvfile, open('nydata.csv', 'w') as output:
rowreader = csv.reader(csvfile)
fieldnames = ['CHURN/LOYAL', 'Medlemstype', 'Alder', 'Kon', 'Kommune', 'Uddannelsesnavn', 'Uddannelsessted', 'Land', 'Ledighed Historik', 'Telefon', 'Mobil', 'SamtaleType', 'Samtalested', 'Samtale maned', 'Churn maned', 'Dagpengeret maned', 'indmeldeses maned', 'fodselsdags maned']
writer = csv.DictWriter(output,delimiter= ',',fieldnames=fieldnames)
#writer= csv.DictWriter.writeheader(fieldnames)
for row in rowreader:
print(row[7])
if row[7] == "Danmark":
writer.writerow(row)
Upvotes: 1
Views: 1105
Reputation: 2541
I would prefer using csv.DictReader
. DictReader converts every row into a dictionary. This helps a lot as the script would still work even if the column order is changed, or any column is inserted/removed in between.
Your code will look like this when you use DictReader.
import csv #import the module csv
with open('akassedatareduced.csv', encoding="ISO-8859-1") as csvfile, open('nydata.csv', 'w') as output:
rowreader = csv.DictReader(csvfile)
fieldnames = ['CHURN/LOYAL', 'Medlemstype', 'Alder', 'Kon', 'Kommune', 'Uddannelsesnavn', 'Uddannelsessted', 'Land', 'Ledighed Historik', 'Telefon', 'Mobil', 'SamtaleType', 'Samtalested', 'Samtale maned', 'Churn maned', 'Dagpengeret maned', 'indmeldeses maned', 'fodselsdags maned']
writer = csv.DictWriter(output,delimiter= ',',fieldnames=fieldnames)
#writer= csv.DictWriter.writeheader(fieldnames)
for row in rowreader:
print(row["Land"]) # Access using column name
if row["Land"] == "Danmark": # Access using column name
writer.writerow(row)
Upvotes: 0
Reputation: 164733
It looks like you have a malformed row. To debug this for yourself, use a try
/ except
clause to catch IndexError
:
for row in rowreader:
try:
if row[7] == "Danmark":
writer.writerow(row)
except IndexError:
print(row)
Then, either let the errors pass silently, or fix your underlying data and rerun.
Upvotes: 2