2015-10-02 102 views
2

我有這個有些問題,我不斷收到:返回AttributeError的:「詮釋」對象有沒有屬性「編碼」

AttributeError: 'int' object has no attribute 'encode'

當我運行它。

我認爲UTF-8將是這個。訂戶只會返回數字或NoneTypes。

任何幫助將不勝感激。

import urllib2,time,csv,json,requests,urlparse,pdb 



SEARCH_URL = urllib2.unquote("http://soyuz.elastic.tubularlabs.net:9200/intelligence_v2/channel_intelligence/%s") 

reader = csv.reader(open('input.csv', 'r+U'), delimiter=',', quoting=csv.QUOTE_NONE) 
#cookie = {"user": "2|1:0|10:1438908462|4:user|36:eyJhaWQiOiA1Njk3LCAiaWQiOiA2MzQ0fQ==|b5c4b3adbd96e54833bf8656625aedaf715d4905f39373b860c4b4bc98655e9e"} 

myfile = open('accounts.csv','w') 

writer = csv.writer(myfile, quoting=csv.QUOTE_MINIMAL) 

processCount = 1 
idsToProcess = [] 
for row in reader: 
    if len(row)>0: 
     idsToProcess.append(row[0]) 
#idsToProcess = ['fba_491452930867938'] 
for userID in idsToProcess: 
# print "fetching for %s.." % fbid 
    url = SEARCH_URL % userID 
    facebooksubscribers = None 
    Instagramsubscribers = None 
    vinesubscribers = None 

    response = requests.request("GET", url) 
    ret = response.json() 
    titleResponse = ret['_source']['title'] 

    try: 
     facebooksubscribers = ret['_source']['facebook']['subscribers']   
    except: 
     facebooksubscribers = " " 

    try: 
     instagramsubscribers = ret['_source']['instagram']['subscribers'] 
    except: 
     instagramsubscribers = " " 

    try: 
     vinesubscribers = ret['_source']['vine']['subscribers'] 
    except: 
     vinesubscribers = " " 



    time.sleep(0) 

    row = [s.encode('utf-8') for s in [userID,titleResponse,facebooksubscribers,instagramsubscribers,vinesubscribers]] 
    writer.writerow(row) 

    #writer.writerow([userID,titleResponse,facebooksubscribers,instagramsubscribers,vinesubscribers,twitterURL]) 

    myfile.flush() 

    print u"%s,%s,%s,%s,%s,%s" % (processCount,userID,titleResponse,facebooksubscribers,instagramsubscribers,vinesubscribers) 
    processCount += 1 
    #print sumEngs 

    #print vidToEngs 
    #print sum(vidToEngs.values()) 
myfile.close() 
exit() 
+1

,因爲這些'[userID,titleResponse,facebooksubscribers,instagramsubscribers,vinesubscribers]'元素中的一個是int,並且您不能對int執行編碼操作。你可能想要在for循環中進行類型轉換。用'row = [str(s).encode('utf-8')替換'user =', s in [userID,titleResponse,facebooksubscribers,instagramsubscribers,vinesubscribers]]' – Pramod

+0

@Pramod你應該在你的解決方案中發佈一個答案,這對我來說也是完全有效的。 – Val

+0

@Pramod感謝您的提示。現在我在這裏得到另一個錯誤:SyntaxError:第49行的SubscribersPullCrossPlatform.py文件中的非ASCII字符'\ xe2',但沒有聲明編碼; ' – Waveformer

回答

4

因爲這些

[userID,titleResponse,facebooksubscribers,instagramsubscribers,vinesubscribers]‌ 

元素的一個數據類型爲int,你不能對INT進行編碼操作。你可能想要在for循環中進行類型轉換。更換

row = [s.encode('utf-8') for s in [userID,titleResponse,facebooksubscribers,instagramsubscribers,vinesubscribers]]‌​ 

row = [str(s).encode('utf-8') for s in [userID,titleResponse,facebooksubscribers,instagramsubscribers,vinesubscribers]]‌

+0

似乎仍然存在一些問題。運行這樣的腳本時,我發現編碼是一個敏感區域。 – Waveformer

+0

@Waveformer現在這是不同的問題,調試這個問題,我必須在我身邊模擬你的問題。您必須查看字符串中正在執行編碼操作的字符類型。但請務必在做對話時不要聽到任何噪音。 – Pramod

+0

有趣的是它成功引發錯誤之前做一個: '1,Y30JRSgfhYXA6i6xX1erWg,了Smosh,6947355,1806850,412974 回溯(最近通話最後一個): 文件 「SubscribersPullCrossPlatform.py」 51行,在 在[userID,titleResponse,facebooksubscribers,instagramsubscribers,vinesubscribers]中爲row = [str(s).encode('utf-8')] UnicodeEncodeError:'ascii'編解碼器無法編碼0-3位置的字符:ordinal不在範圍內(128) (venv)Maxs-MacBook-Pro:排行榜腳本maxramsay $' – Waveformer

0

使用此:

repr(s).encode('utf-8') 

代替:

s.encode('utf-8') 
相關問題