我有2個API。我從他們那裏獲取數據。我想將特定的代碼部分分配給字符串,以便在編碼時使生活變得更容易。下面是代碼:TypeError:強制轉換爲Unicode:需要字符串或緩衝區,發現int找到
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
你可以包括完整的追溯? – selllikesybok 2014-12-03 14:55:17
肯定的:' 回溯(最近通話最後一個): 文件 「C:/Users/gnite_000/Desktop/test.py」 25行,在 名+ 「的應付利息 - 」 + interestPayable 類型錯誤:強制爲Unicode:需要字符串或緩衝區,int找到 ' –
2014-12-03 14:57:18
爲什麼你要做所有'name +'的interest interest - '+'語句?他們會被扔在當前的代碼中。 – selllikesybok 2014-12-03 15:03:15