Before anyone marks this as duplicate, I have tried everything from isspace, startswith, itertools filterfunction, readlines()[2:]. I have a Python script that searches hundreds of CSV files and prints the row with the matching string (in this case a unique ID) in the eighth column from the left.
import csv
import glob
csvfiles = glob.glob('20??-??-??.csv')
for filename in csvfiles:
reader = csv.reader(open(csvfiles))
for row in reader:
col8 = str(row[8])
if col8 == '36862210':
print row
The code works with test .csv files. However, the real .csv files I'm working with all have blank first two rows. And I am getting this error message.
IndexError: list index out of range
Here's my latest code:
import csv
import glob
csvfiles = glob.glob('20??-??-??.csv')
for filename in csvfiles:
reader = csv.reader(open(csvfiles))
for row in reader:
if not row:
continue
col8 = str(row[8])
if col8 == '36862210':
print row
解决方案
Try to skip the first two row using next instead:
import csv
import glob
csvfiles = glob.glob('20??-??-??.csv')
for filename in csvfiles:
reader = csv.reader(open(filename))
next(reader)
next(reader)
for row in reader:
col8 = str(row[8])
if col8 == '36862210':
print row