from csv import reader
data_rdr = reader(open('mn.csv','rb'))
header_rdr = reader(open('mn_headers.csv','rb'))
data_rows = [d for d in data_rdr]
header_rows = [h for h in header_rdr]
bad_rows = []
for h in header_rows:
if h[0] not in data_rows[0]:
bad_rows.append(h)
for h in bad_rows:
header_rows.remove(h)
print len(header_rows)
Traceback (most recent call last):
File "/home/sewell-hoo/datafiles/test_listindex.py", line 12, in <module>
if h[0] not in data_rows[0]:
IndexError: list index out of range
正在读《python数据处理》一书,这是第7章第2小节的内容:
处理2个CSV文件,却一直提示List index out of range,实在是想不到哪里错了。
请忽略图中配图的文件'mn_header_update.csv',两者是一个文件