Python 类型错误:强制转换为 Unicode:需要字符串或缓冲区,找到 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27274705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:36:01  来源:igfitidea点击:

TypeError: coercing to Unicode: need string or buffer, int found

pythonapiunicodebuffertypeerror

提问by Marks Gniteckis

I have 2 APIs. I am fetching data from them. I want to assign particular code parts to string so that life became easier while coding. Here is the code:

我有 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

In result I get:

结果我得到:

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

采纳答案by TravelingMaker

The problem might have to do with the fact that you are adding ints to strings here

问题可能与您在此处向字符串添加整数有关

    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

As far as I'm aware, the interpretor cannot implicitly convert an int to a string. This might work, though,

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

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

On which I'm assuming Python > 3.0

我假设 Python > 3.0

回答by Maryam Homayouni

You have to add '%s' % and () to each line, like this:

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

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