在csv阅读器中遇到一个奇怪的问题。
这是我的csv列表。 '维吉尔'有3个项目,是在玛丽亚'
之后_timesheet.csv(略)
maria,2018-02-08 20:30:04.180000,45.0
maria,2018-02-15 20:30:04.180000,45.0
maria,2018-02-22 20:30:04.180000,45.0
maria,2018-03-01 20:30:04.180000,45.0
virgil,2018-02-08 20:30:04.180000,45.0
virgil,2018-02-15 20:24:41.264000,45.0
virgil,2018-02-22 20:24:41.264000,45.0
virgil,2018-03-01 20:24:41.264000,45.0
但是当我处理它时,结果会变成这个并导致索引超出范围。
['merry', '2018-02-08 09:09:09.315000', '45.0']
['merry', '2018-02-15 09:09:09.315000', '45.0']
['merry', '2018-02-22 09:09:09.315000', '45.0']
['merry', '2018-03-01 09:09:09.315000', '45.0']
['virgil', '2018-02-08 09:09:09.3150']
IndexError: timesheetF[2] index out of range
'快活'位于更远的位置,但在此过程中,它就变得紧挨着它。
我遗失的任何流程?下面是我的代码
_resources = list(csv.reader(open("raw/resources.csv", 'rb')))
_timesheets = list(csv.reader(open("raw/_timesheet.csv", 'rb')))
for resource in _resources:
for timesheetF in _timesheets:
res_row = []
if(resource[0] == timesheetF[0]):
res_row.append(resource[0])
if 'philippines' in resource[1].lower():
res_row.append("Manila")
else:
res_row.append(resource[1])
#Active
if 'true' in resource[2].lower():
res_row.append("Yes")
else:
res_row.append("No")
if 'kronos' in resource[3].lower():
res_row.append("KRO")
elif 'avenew' in resource[3].lower():
res_row.append("POS")
elif 'hcm' in resource[3].lower():
res_row.append("LND")
else:
res_row.append(resource[3])
if 'support and maintenance' in resource[4].lower():
res_row.append("AM")
elif 'development' in resource[4].lower():
res_row.append("AD")
elif 'admin' in resource[4].lower():
res_row.append("Admin")
res_row.append(timesheetF[2]) ###### ERROR HERE ###
results.writerow(res_row)
答案 0 :(得分:0)
如果没有看到_timesheet.csv文件,我最好的猜测是,你的时间表csv文件中的一行只包含2个字段,通常应该有3个。
错误通知您索引2超出了正在处理的当前时间表F的可用值范围。我会尝试找出_timesheet.csv中的哪一行是导致错误的行,然后继续。
您可以在res_row.append(timesheetF [2])行周围输入try / except语句,并打印出timesheetF值以帮助调试问题。