“int”对象不是Python2.7的可下标对象

2024-06-28 15:52:02 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我是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....."  

Tags: nameinforrecordmappingfilerowworkbook
1条回答
网友
1楼 · 发布于 2024-06-28 15:52:02

您在代码中使用名称mapping_record表示两种不同的内容:

# a custom object
mapping_record = MappingRecord()

# as a loop variable
for mapping_record in mapping_records:

在循环中,它是mapping_records字典中的一个,并且您的键是整数,因此当您尝试使用mapping_record[4]时,会得到一个异常。你知道吗

也许你想在字典中循环使用?在这种情况下,请使用:

for mapping_record in mapping_records.values():

相关问题 更多 >