Python 类型错误:';int';对象不可下标,其中为int?
我收到一个错误,Python 类型错误:';int';对象不可下标,其中为int?,python,django,excel,Python,Django,Excel,我收到一个错误,TypeError:“int”对象不可下标。 我想连接2个excel数据 根据用户模型 所以我的理想输出是 1|1|Blear|40|false|l|America|A|1 2|5|Tom|23|true|o|UK|A|3 3|9|Rose|52|false|m 4|10|Karen||||Singapore|C|2 例如,用户_id=3的Rose数据不在第二个excel中,在这种情况下,第二个数据为空是可以的。我想将第二个excel放在用户模型的字典类型中 我搜索了错误,
TypeError:“int”对象不可下标
。
我想连接2个excel数据
根据用户模型
所以我的理想输出是
1|1|Blear|40|false|l|America|A|1
2|5|Tom|23|true|o|UK|A|3
3|9|Rose|52|false|m
4|10|Karen||||Singapore|C|2
例如,用户_id=3的Rose数据不在第二个excel中,在这种情况下,第二个数据为空是可以的。我想将第二个excel放在用户模型的字典类型中
我搜索了错误,我认为这部分查找数据目录中的数据是错误的,我将其更改为查找范围内的数据(len(数据目录))
,但同样的错误也发生了。我真的不明白哪里出了错。我该如何修复
现在views.py是
#coding:utf-8
from django.shortcuts import render
import xlrd
from .models import User
book = xlrd.open_workbook('../data/excel1.xlsx')
sheet = book.sheet_by_index(1)
def build_employee(employee):
if employee == 'leader':
return 'l'
if employee == 'manager':
return 'm'
if employee == 'others':
return 'o'
for row_index in range(sheet.nrows):
rows = sheet.row_values(row_index)
is_man = rows[4] != ""
emp = build_employee(rows[5])
user = User(user_id=rows[1], name_id=rows[2], name=rows[3],
age=rows[4],man=is_man,employee=emp)
user.save()
book2 = xlrd.open_workbook('../data/excel2.xlsx')
sheet2 = book2.sheet_by_index(0)
headers = sheet2.row_values(0)
large_item = None
data_dict = {}
for row_index in range(sheet2.nrows):
rows2 = sheet2.row_values(row_index)
large_item = rows2[1] or large_item
# Create dict with headers and row values
row_data = {}
for idx_col, value in enumerate(rows2):
header_value = headers[idx_col]
# Avoid to add empty column. A column in your example
if header_value:
row_data[headers[idx_col]] = value
# Add row_data to your data_dict with
data_dict[row_index] = row_data
for row_number, row_data in data_dict.items():
user1 = User.objects.filter(user_id = data['user_id']).exists()
if user1:
user1.__dict__.update(**data_dict)
user1.save()
现在回溯已经开始
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/XXX/testapp/app/views.py", line 123, in <module>
user1 = User.objects.filter(user_id = row_data['user_id']).exists()
KeyError: 'user_id'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/Users/XXX/testapp/app/views.py”,第123行,在
user1=User.objects.filter(User\u id=row\u data['User\u id'])。exists()
KeyError:“用户\u id”
数据
是一个整数。因此,像dict一样调用数据会引起这种期望
>>> a=1
>>> a['a']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable
尝试使用items()
如下:
>>> for key, value in a.items(): print(key, value)
...
1 x
2 c
或者,在您的具体情况下:
for row_number, row_data in data_dict.items():
print(row_number, row_data)
有关详细信息,请参阅。数据是一个整数。因此,像dict一样调用数据会引起这种期望
>>> a=1
>>> a['a']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable
尝试使用items()
如下:
>>> for key, value in a.items(): print(key, value)
...
1 x
2 c
或者,在您的具体情况下:
for row_number, row_data in data_dict.items():
print(row_number, row_data)
有关详细信息,请参阅。此错误的堆栈跟踪是什么?发布完整的日志stacktrace@birryree我添加了回溯,如果你知道什么,请帮助我。@Exprator我添加了回溯,如果你知道什么,请帮助我。我想你没有跳过标题<代码>数据['user\u id']
正试图转换int('user\u id'),因为user\u id
是int,以db表示。此错误的堆栈跟踪是什么?发布完整的stacktrace@birryree我添加了回溯,如果你知道什么,请帮助我。@Exprator我添加了回溯,如果你知道什么,请帮助我。我想你没有跳过标题<代码>数据['user\u id']
正试图转换int('user\u id'),因为user\u id
在db中是int。如果“他/她在data['user\u id']
中得到一个键错误,在这种情况下?”keyrerror是当数据是dict但在其中找不到键时。这是有意义的。@jpic thx ur回答。我写了你的代码,但出现了新的错误。我更新了我的问题(views.py和models.py)我是否误解了你的答案?如果你知道一些事情,请帮助我。如果“他/她在data['user\u id']
中得到一个关键错误,在这种情况下?”关键错误是当数据是一个dict但在其中找不到关键时。这是有意义的。@jpic thx你的答案。我写了你的代码,但出现了新的错误。我更新了我的问题(views.py和models.py)我是否误解了你的答案?如果你知道什么,请帮助我。