迭代器每次使用后会迭代至末尾
当我们再次使用这个迭代器进行for循环读取时得到的结果自然为空
解决方法只需新建一个迭代器即可。
迭代器:IndexError: list index out of range
import csv
#读T1
reader = csv.reader(open('结果1.csv','rt'))
column1 = [row1[0:9] for row1 in reader]
# 写入标题
with open('demo2.csv', 'w', encoding='UTF8', newline='') as f0:
writer0 = csv.writer(f0)
# write the header
writer0.writerow(column1[0])
#读T2
reader2 = csv.reader(open('结果1.csv', 'rt'))
column2 = [row2[0:3]+row2[9:15] for row2 in reader2]
#读T3
reader3 = csv.reader(open('结果1.csv', 'rt'))
column3 = [row3[0:3]+row3[15:21] for row3 in reader3]