1

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)
2
  • 1
    can you print rowreader? Commented Oct 29, 2018 at 10:12
  • This is because some rows do not have an 8th column (remember that array numbering starts from 0). Please confirm if all rows have the 8th column. A better approach would be to use DictReader (docs.python.org/2/library/csv.html#csv.DictReader) and then access the column like row['ColumnName'] == 'Danmark'. Commented Oct 29, 2018 at 10:48

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)

3 Comments

Thanks helps me a bit further, i get no errors and it looks at the right column through the rows. However, the printed output says "none" and loops through all the rows with "Danmark".for row in rowreader: try: print(row["Land"]) # Access using column name if row["Land"] == "Danmark": writer.writerow(row) except IndexError: print(row)`
Try printing row with print row and post the output.
This is the output when printing the print(row) Values are none OrderedDict([('CHURN/LOYAL', 'CHURN,Betalende,63,M,Gladsaxe Kommune,cand.polyt,Ikke pa liste,Danmark,Ingen Ledighedshistorik,Udfyldt,Ikke udfyldt,,,,"December, 1989","September, 1950","March, 1977","January, 1926"'), ('Medlemstype', None), ('Alder', None), ('Kon', None), ('Kommune', None), ('Uddannelsesnavn', None), ('Uddannelsessted', None), ('Land', None), ('Ledighed Historik', None), ('Telefon udfyldt status', None), ('Mobil udfyldt status', None), ('Samtaletype', None), ('Samtalested', None), ('Samtale Maned', None)

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.