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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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