【问题标题】:TypeError: coercing to Unicode: need string or buffer, int foundTypeError:强制转换为 Unicode:需要字符串或缓冲区,找到 int
【发布时间】:2015-02-01 04:36:43
【问题描述】:

我有 2 个 API。我正在从他们那里获取数据。我想将特定的代码部分分配给字符串,以便在编码时变得更轻松。代码如下:

import urllib2
import json

urlIncomeStatement = 'http://dev.c0l.in:8888'
apiIncomeStatement = urllib2.urlopen(urlIncomeStatement)
dataIncomeStatement = json.load(apiIncomeStatement)

urlFinancialPosition = 'http://dev.c0l.in:9999'
apiFinancialPosition = urllib2.urlopen(urlFinancialPosition)
dataFinancialPositiont = json.load(apiFinancialPosition)

for item in dataIncomeStatement:
    name = item['company']['name']
    interestPayable = int(item['company']['interest_payable'])
    interestReceivable = int(item['company']['interest_receivable'])
    sales = int(item['company']['interest_receivable'])
    expenses = int(item['company']['expenses'])
    openingStock = int(item['company']['opening_stock'])
    closingStock = int(item['company']['closing_stock'])
    sum1 = sales + expenses

    if item['sector'] == 'technology':
        name + "'s interest payable - " + interestPayable
        name + "'s interest receivable - " + interestReceivable
        name + "'s interest receivable - " + sales
        name + "'s interest receivable - " + expenses
        name + "'s interest receivable - " + openingStock
        name + "'s interest receivable - " + closingStock

print sum1

结果我得到:

Traceback (most recent call last):
  File "C:/Users/gnite_000/Desktop/test.py", line 25, in <module>
    name + "'s interest payable - " + interestPayable
TypeError: coercing to Unicode: need string or buffer, int found

【问题讨论】:

  • 能否包含完整的回溯?
  • 当然:` Traceback(最近一次通话最后一次):文件“C:/Users/gnite_000/Desktop/test.py”,第 25 行,在 名称中 +“的应付利息 - " + interestPayable TypeError: coercing to Unicode: need string or buffer, int found `
  • 你为什么要做所有这些name + 's interest receivable - ' + 声明?它们会在您当前的代码中被丢弃。

标签: python api unicode buffer typeerror


【解决方案1】:

问题可能与您在此处将整数添加到字符串这一事实有关

    if item['sector'] == 'technology':
        name + "'s interest payable - " + interestPayable
        name + "'s interest receivable - " + interestReceivable
        name + "'s interest receivable - " + sales
        name + "'s interest receivable - " + expenses
        name + "'s interest receivable - " + openingStock
        name + "'s interest receivable - " + closingStock

据我所知,解释器无法将 int 隐式转换为字符串。 不过,这可能会奏效,

       str(name) + "'s interest receivable - " + str(closingStock)

我假设 Python > 3.0

【讨论】:

  • 如果我删除 int(),我会得到同样的错误,但不是 in found,而是 float found
  • @MarksGniteckis 是的,因为在序列化对象中,您指向的数据是浮点数,而不是字符串。您将其转换为 int,然后添加到字符串。 float 和 int 都不能添加到字符串中。只需将它们包装在 str() 中,例如 + str(interestPayable)
  • 好的,为问题添加了不同的解决方案。 selllikesybok 说的是哪个。
  • “据我所知,解释器无法将 int 隐式转换为字符串”:这不是限制,而是设计使然。
  • 另外,正确的解决方案是使用字符串格式(参见 Python 版本的精美 Python 手册)。
【解决方案2】:

您必须在每一行添加 '%s' % 和 (),如下所示:

'%s' % (name + "'s interest payable - " + interestPayable)

【讨论】:

  • 试试这个。 Python 2.7 - 这对某些人来说似乎更有说服力。用 %s " %s 's interest receivable - %s"%(name, closingStock) 包装引号和替换变量之间的所有内容
  • 这个答案将导致相同的强制。正如@ajaykools 在 cmets 中所建议的,应该将其更改为类似于“%s 的应收利息 - %s”% (name, interestPayable)。
猜你喜欢
  • 2021-03-28
  • 2014-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-17
  • 2011-10-04
相关资源
最近更新 更多