Python Forum
csv reader Index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
csv reader Index out of range
#1
I work with large CSV files that often times are too large to open in Excel. I'm trying to write a program that will extract a dozen or so columns from the 1460 columns in the file. I know the header names that I need, but I cannot get my parse program to work.
import csv

datafile = open("C:\\Users\\Administrator\\Desktop\\test.csv",'r')
reader = csv.reader(datafile,delimiter=',')
  
outfile= open("C:\\Users\\Administrator\\Desktop\\csv.csv",'w')
writer = csv.writer(outfile, delimiter=',')

header=[]
parameter=['Rec #','AirSpd_ADC-1','AirSpd_ADC-2','AirTemp-1','AirTemp-2']

i=0
for row in reader: # get index of header names for iterating
    if i == 8:
        for name in row:
            if name in parameter:
                header.append(row.index(name))
    i+=1
    
datafile.seek(0)  # reset to use csv reader again

for row in reader:
    for col in row:
        for indx in header:
            outfile.write(row[indx])
Error:
>>IndexError: string index out of range
Reply
#2
You need to format your post with the code tags to make it readable: https://python-forum.io/misc.php?action=help&hid=25

Also, does your IndexError provide a stack trace that says which line is blowing up?
Reply
#3
Without a proper code formatting, the least I can see is that you are using the reader iterable in two for loops. But after the first one, it is exhausted already.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
My apologies for the format issue guys. My first post. Mpd,the trace back is to the code: outfile.write(row[indx]). Wavic, you are correct the csv reader is exhausted in the first for reader loop, but after hours of searching online I found a thread with the .seek(0) method. Best I can tell it resets the file to the top so that the reader can be iterated over again. And it works.

After drawing a diagram of the code last night I am certain that there are too many "for" loops in the last for statement.
Reply
#5
you are over-complicating something simple as

import csv

infile = 'infile.csv' # file with large number of columns, dummy filednames field1, field2...field1460
outfile = 'outfile.csv' # you need just 3 out of 1460 columns field2, field10, filed25, in that order

fieldnames = ['field2', 'field10', 'field25']

with open(infile, 'r') as in_f, open(outfile, 'w', newline='') as out_f:
    rdr = csv.DictReader(in_f)
    wrtr = csv.DictWriter(out_f, fieldnames=fieldnames, extrasaction='ignore')
    wrtr.writeheader()
    for row in rdr:
        wrtr.writerow(row)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Down I hate "List index out of range" Melen 20 3,307 May-14-2023, 06:43 AM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 6,318 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  xml simple reader kucingkembar 2 1,053 Aug-19-2022, 08:51 PM
Last Post: kucingkembar
  IndexError: list index out of range dolac 4 1,902 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,334 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,437 May-03-2022, 01:39 PM
Last Post: Anldra12
  Having strange results from an RFID HID card reader - I'm stuck orbisnz 1 1,477 Mar-28-2022, 08:20 AM
Last Post: Larz60+
  matplotlib x axis range goes over the set range Pedroski55 5 3,185 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,468 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  IndexError: list index out of range rf_kartal 6 2,838 Sep-07-2021, 02:36 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020