ValueError:int()与基数10无效的文字:'013000ab7C8'

我想parsingexcel&制作字典,并把具有相同user_id字典的模型(用户)。 现在我得到一个错误, ValueError: invalid literal for int() with base 10: '013000ab7C8' 。 当然,'013000ab7C8'不是int,但我真的不明白为什么会发生这个错误。 我在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() 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() 

我想连接用户文件夹中的excel文件和user_id中的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), ]) } 

我的代码出了什么问题?我该如何解决这个问题?

如果User模型的user_id字段被指定为IntegerField那么显然不能这样做:

 user = User(user_id=rows[1], name_id=rows[2], name=rows[3], ...) # ^^^^^^^ Needs integer value 

并且:

 user1 = User.filter(user_id=row_data['user_id']).exists() # ^^^^^^^^ Needs integer value 

或者,如果这些值是hex值,并且除af之外不包含其他字母字符,那么您可能需要假设这些值的基数为16,然后利用该值:

 >>> int('013000ab7C8', 16) 81605081032 

不知道这是一个稳定的解决scheme。 您可能想要将这些字段重新指定为直接整数值。

如果你显示你的models.py,那更好。 您可能已经将user_id声明为models.py的User类下的整数字段。 因此你得到这个错误。