2014-12-03 11 views
19

Tôi có 2 API. Tôi đang lấy dữ liệu từ họ. Tôi muốn chỉ định các phần mã cụ thể cho chuỗi để cuộc sống trở nên dễ dàng hơn khi mã hóa. Đây là mã:LoạiError: liên kết với Unicode: cần chuỗi hoặc bộ đệm, int được tìm thấy

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 

Trong kết quả tôi nhận được:

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 
+0

Bạn có thể bao gồm toàn bộ lần truy nguyên không? – selllikesybok

+0

chắc: ' Traceback (gần đây nhất gọi cuối cùng): File "C: /Users/gnite_000/Desktop/test.py", dòng 25, trong tên + "'s lãi phải trả -" + interestPayable Lỗi Loại: xâu chuỗi vào Unicode: cần chuỗi hoặc bộ đệm, int được tìm thấy ' –

+0

Tại sao bạn thực hiện tất cả các câu lệnh lãi phải thu - '+' này? Chúng bị vứt bỏ trong mã hiện tại của bạn. – selllikesybok

Trả lời

26

Vấn đề có thể phải làm với thực tế mà bạn đang thêm ints thành các chuỗi đây

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 như tôi biết, người phiên dịch không thể chuyển đổi int một chuỗi thành chuỗi. này có thể làm việc, tuy nhiên,

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

On mà tôi giả sử Python> 3,0

+0

Nếu tôi xóa int(), tôi sẽ gặp lỗi tương tự, nhưng thay vì tìm thấy, tôi sẽ nhận được float tìm thấy –

+0

@MarksGniteckis yes, bởi vì trong đối tượng được tuần tự hóa, dữ liệu bạn trỏ đến là một phao, không phải là chuỗi . Bạn chuyển đổi nó thành một int, sau đó thêm vào một chuỗi. Cả float và int đều không thể được thêm vào chuỗi. Chỉ cần bọc chúng trong str(), như '+ str (interestPayable)'. – selllikesybok

+0

Được rồi, đã thêm một giải pháp khác cho vấn đề. Mà selllikesybok đã nói về. – TravelingMaker

2

Bạn phải thêm '% s' % và() với từng ngành nghề, như thế này:

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

Điều này không hiệu quả đối với tôi. – FredFury

Các vấn đề liên quan