简体   繁体   English

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

[英]ValueError: invalid literal for int() with base 10: '013000ab7C8'

I wanna parse excel& make dictionary and put the model(User) which has same user_id of dictionary. 我想解析excel&make字典,然后放入具有相同user_id字典的model(User)。 Now I got an error, ValueError: invalid literal for int() with base 10: '013000ab7C8' . 现在我得到一个错误, ValueError: invalid literal for int() with base 10: '013000ab7C8' Of course,'013000ab7C8' is not int,but I really cannot understand why this error happens. 当然,“ 013000ab7C8”不是int,但我真的不明白为什么会发生此错误。 I wrote in views.py 我写在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()

I wanna connect excel files under user's folder & excel1.xlsx in user_id,it means if each data has same user_id,it will be connected. 我想连接user_id中用户文件夹&excel1.xlsx下的excel文件,这意味着如果每个数据都具有相同的user_id,它将被连接。 data_dict_key is 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),
    ])
}

What is wrong my code?How should I fix this? 我的代码有什么问题我该如何解决?

If the user_id field of your User model was specified as an IntegerField you obviously cannot do: 如果将User模型的user_id字段指定为IntegerField您显然无法执行以下操作:

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

and also: 并且:

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

As an alternative, if the values are hex values, and contain no other alpha characters besides af , then you may want to assume the values are in base 16, and exploit that instead: 或者,如果这些值是十六进制值,并且除了af之外不包含其他任何alpha字符,那么您可能要假设这些值位于以16为底的值,并利用该值:

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

Not sure this is a stable solution though. 不确定这是否是一个稳定的解决方案。 You probably want to respecify those fields as direct integer values. 您可能希望将这些字段重新指定为直接整数值。

Better if you show your models.py. 如果显示您的models.py更好。 You might have declared user_id as an integer field under the User class of your models.py. 您可能已经在models.py的User类下将user_id声明为整数字段。 Hence you are getting this error. 因此,您将收到此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM