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


Messages In This Thread
csv reader Index out of range - by mathisp64 - Jan-10-2018, 05:51 PM
RE: csv reader Index out of range - by mpd - Jan-10-2018, 08:22 PM
RE: csv reader Index out of range - by wavic - Jan-10-2018, 09:16 PM
RE: csv reader Index out of range - by mathisp64 - Jan-11-2018, 09:11 PM
RE: csv reader Index out of range - by buran - Jan-11-2018, 09:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Down I hate "List index out of range" Melen 20 3,559 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,771 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  xml simple reader kucingkembar 2 1,109 Aug-19-2022, 08:51 PM
Last Post: kucingkembar
  IndexError: list index out of range dolac 4 1,999 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,429 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,497 May-03-2022, 01:39 PM
Last Post: Anldra12
  Having strange results from an RFID HID card reader - I'm stuck orbisnz 1 1,550 Mar-28-2022, 08:20 AM
Last Post: Larz60+
  matplotlib x axis range goes over the set range Pedroski55 5 3,333 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,538 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  IndexError: list index out of range rf_kartal 6 2,935 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