嗨,我是python新手,正在阅读excel文件。我再次将其插入数组中。 但我犯了个错误
File "SPD_new.py", line 57, in <module>
spd_record.transdate = str(mapping_record[4])
TypeError: 'int' object is not subscriptable
下面是我的代码:
workbook = xlrd.open_workbook("SPD.xlsx")
worksheets = workbook.sheet_names()
mapping_records = {}
for worksheet_name in worksheets:
worksheet = workbook.sheet_by_name(worksheet_name)
mapping_record = MappingRecord()
if worksheet_name == "NB":
for curr_row in range(0,worksheet.nrows):
mapping_record = worksheet.row(curr_row)
mapping_records[0] = mapping_record
print worksheet_name
print mapping_record[1]
for mapping_record in mapping_records:
print "In If...."
spd_record = MappingRecord()
spd_record.merchantid = "00002269"
spd_record.transdate = mapping_record[4]
spd_record.amount = float(mapping_record[5])
spd.erocode = None
spd.scno = None
mapping_records[spd_record[8]] = spd_record
print spd_record
elif worksheet_name == "CD":
for curr_row in range(0,worksheet.nrows):
mapping_records[0] = mapping_record
print worksheet_name
print "Read SPD File....."
您在代码中使用名称
mapping_record
表示两种不同的内容:在循环中,它是
mapping_records
字典中的一个键,并且您的键是整数,因此当您尝试使用mapping_record[4]
时,会得到一个异常。你知道吗也许你想在字典中循环使用值?在这种情况下,请使用:
相关问题 更多 >
编程相关推荐