【问题标题】:invalid literal for int() with base 10: '' when creating object基数为 10 的 int() 的无效文字:'' 创建对象时
【发布时间】:2016-11-10 05:17:40
【问题描述】:

我正在尝试从 CSV 文件中读取并在 Django(1.9,Py 3.5)中创建一个对象,但无论我将字段更改为什么,我都会收到此错误

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

那一行是:

其他 = 行['其他']

site = Site.objects.create(consolidated_financials      = row['Consolidated financials'],
                                  type                          = Type.objects.get_or_create(name=row['Type'])[0],
                                  tier1_business                = Business.objects.get_or_create(tier=1, name=row['Tier-1 business'])[0],
                                  tier2_business                = Business.objects.get_or_create(tier=2, name=row['Tier-2 business'])[0],
                                  tier3_business                = Business.objects.get_or_create(tier=2, name=row['Tier-3 business'])[0],
                                  site_name                     = row['Site Name'],
                                  site_id                       = row['Site ID'],
                                  region                        = Region.objects.get_or_create(name=row['Region'])[0],
                                  country                       = Country.objects.get_or_create(name=row['Country'], region=Region.objects.get_or_create(name=row['Region'])[0])[0],
                                  city                          = City.objects.get_or_create(name=row['City'], country=Country.objects.get_or_create(name=row['Country'], region=Region.objects.get_or_create(name=row['Region'])[0])[0])[0],
                                  site_type                     = SiteType.objects.get_or_create(name=row['Type of site?'])[0],
                                  remote_site                   = row['Remote site?'],
                                  finance_manager_name          = row['Finance Manager Name'],
                                  finance_manager_sso           = row['Finance Manager SSO'],
                                  quarter                       = row['Quarter'],
                                  revenue                       = row['Revenue'],
                                  supply_chain_manager_name     = row['Supply Chain Manager Name'],
                                  supply_chain_manager_sso      = row['Supply Chain Manager SSO'], 
                                  product_lines                 = row['Product Lines'],
                                  manufacturing_processes       = row['Manufacturing Processes'],
                                  factory_utilization           = row['Factory Utilization'],
                                  fte                           = row['FTE'],
                                  hourly                        = row['Hourly'],
                                  salaried                      = row['Salaried'],
                                  other                         = row['Other']
                                  ) 

站点模型:

class Site(models.Model):
"""
Model for a site entry
@author: Leonardo Pessoa
@since: 05/09/2016 
"""
from decimal import Decimal

consolidated_financials     = models.BooleanField()
type                        = models.ForeignKey(Type)
tier1_business              = models.ForeignKey(Business, limit_choices_to = {'tier': 1}, related_name='%(class)s_tier1')
tier2_business              = models.ForeignKey(Business, limit_choices_to = {'tier': 2}, related_name='%(class)s_tier2')
tier3_business              = models.ForeignKey(Business, limit_choices_to = {'tier': 3}, related_name='%(class)s_tier3')
site_name                   = models.CharField(max_length = 150, unique=True)
site_id                     = models.IntegerField()
region                      = models.ForeignKey(Region)
country                     = models.ForeignKey(Country)
city                        = models.ForeignKey(City)
site_type                   = models.ForeignKey(SiteType)
remote_site                 = models.BooleanField()
finance_manager_name        = models.CharField(max_length = 50)
finance_manager_sso         = models.IntegerField()
quarter                     = models.DecimalField(max_digits = 12, decimal_places = 2, default=Decimal('0.0'))
revenue                     = models.DecimalField(max_digits = 12, decimal_places = 2, default=Decimal('0.0'))
supply_chain_manager_name   = models.CharField(max_length = 50, default='')
supply_chain_manager_sso    = models.IntegerField(default=000000000)
product_lines               = models.CharField(max_length = 100, default='')
manufacturing_processes     = models.TextField(max_length = 500, default='')
factory_utilization         = models.DecimalField(max_digits = 5, decimal_places = 2, default=Decimal('0.0'))
fte                         = models.IntegerField()
hourly                      = models.IntegerField()
salaried                    = models.IntegerField()
other                       = models.TextField(max_length = 500, default='')
ges_id                      = models.CharField(max_length = 20)
latitude                    = models.DecimalField(max_digits = 10, decimal_places=7, default=Decimal('0.0'))
longitude                   = models.DecimalField(max_digits = 10, decimal_places=7, default=Decimal('0.0'))

行:

row 
{'City': 'xxxxxxx',
 'Consolidated financials': 'True',
 'Country': 'Argentina (AR)',
 'FTE': '',
 'Factory Utilization': '',
 'Finance Manager Name': '',
 'Finance Manager SSO': '',
 'Hourly': '',
 'Manufacturing Processes': '',
 'Other': '',
 'Product Lines': '',
 'Quarter': '',
 'Region': 'Latin America',
 'Remote site?': 'True',
 'Revenue': '',
 'Salaried': '',
 'Site ID': '12312',
 'Site Name': 'xxxxxxxxx',
 'Supply Chain Manager Name': '',
 'Supply Chain Manager SSO': '',
 'Tier-1 business': 'xxxxxxxxxx',
 'Tier-2 business': 'xxxxxxxxxxxxx',
 'Tier-3 business': 'Latin America',
 'Type': 'xxxxxx xxxxx',
 'Type of site?': 'Other'}

我知道代码有很大的性能优化空间,但我只想先证明功能。

谢谢!

【问题讨论】:

  • 您需要将 other 转换为 int。如果通常是代表数字的字符串,可以int(other),但如果是空字符串,则可能需要将其替换为0
  • @joelgoldstick 我刚刚添加了模型以供参考。它需要文本。
  • 但不知何故,它得到了一个空字符串。查看您的 csv 文件中的相关行
  • @joelgoldstick 它是,它是空的。我不能在那里有一个空字符串吗?
  • 是的,您将其声明为文本。那么,能否请您显示完整的回溯,以及它所引用的代码

标签: python django model


【解决方案1】:

问题是您的Site 模型期望otherint(该模型是否有other = IntegerField 或类似的?),而您提供的是一个空字符串。最简单的解决方法是将row['Other'] 更改为row['Other'] or 0

如果您知道将获取非数字值作为一般规则,那么您可以添加基本逻辑来测试非数字,或者将您的 IntegerField 更新为可以接受文本的内容。 A list of valid Django fields can be found here.

# An example of conditional logic to test for a non-number and use 0 if so
other = row['Other'] if row['Other'] and row['Other'].isdigit() else 0

编辑

查看您的模型,问题可能不在于“其他”字段,但仍然存在打字问题。例如 Supply Chain Manager SSO 应该是一个 int,但你肯定传递了一个 ''

【讨论】:

  • 感谢您的回复。我添加了模型供参考,它是一个 TextField。
  • 就在现场!问题在于我的 CSV 和预期字段中的不同类型。由于该行特别没有问题,因此很难通过错误来阅读。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2013-05-31
  • 2018-03-13
  • 1970-01-01
  • 2021-06-25
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多