我有两个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
name + 's interest receivable - ' +
语句?它们在你当前的代码中被抛弃了。 - selllikesybok