我想練成解析化妝&字典,把具有字典的相同user_ID的模型(用戶)。現在我收到了一個錯誤,ValueError: invalid literal for int() with base 10: '013000ab7C8'
。 當然,'013000ab7C8'不是int,但我真的不明白爲什麼會發生這個錯誤。 我views.pyValueError異常:無效的基數爲10字面INT():「013000ab7C8」
#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()
files = glob.glob('./user/*.xlsx')
data_dict_key ={}
for x in files:
if "$" not in x:
book3 = xlrd.open_workbook(x)
sheet3 = book3.sheet_by_index(0)
cells = [
]
data_dict = OrderedDict()
for key, rowy, colx in cells:
try:
data_dict[key] = sheet3.cell_value(rowy, colx)
except IndexError:
data_dict[key] = None
if data_dict['user_id'] in data_dict_key:
data_dict_key[data_dict['user_id']].update(data_dict)
continue
data_dict[data_dict_key['user_id']] = data_dict
for row_number, row_data in data_dict_key.items():
user1 = User.filter(user_id=row_data['user_id']).exists()
if user1:
user1.__dict__.update(**data_dict_key)
user1.save()
寫道,我想在 USER_ID連接Excel文件在用戶的文件夾& excel1.xlsx,這意味着如果每個數據都有相同的USER_ID,它將連接。 data_dict_key是
dicts = {
'013000ab7C8: OrderedDict([
('user_id', '013000ab7C8'),
('name_id', 'Blear'),
('nationality', 'America'),
('domitory', 'A'),
('group', 1),
]),
'088009cd1W9': OrderedDict([
('user_id', '088009cd1W9'),
('name_id', 'Tom'),
('nationality': 'UK'),
('domitory': 'B'),
('group': 18),
])
}
什麼是錯我的代碼?我應該怎樣解決這個問題?
你可以提供錯誤的堆棧跟蹤來將它連接到你的代碼嗎? –