返回AttributeError:'int'对象没有属性'encode'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了返回AttributeError:'int'对象没有属性'encode'相关的知识,希望对你有一定的参考价值。

我遇到了一些问题,我不断得到:

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()
答案

因为其中之一

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

element是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]]‌

另一答案

用这个 :

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

代替 :

s.encode('utf-8')

以上是关于返回AttributeError:'int'对象没有属性'encode'的主要内容,如果未能解决你的问题,请参考以下文章

python的密码学抛出AttributeError:'int'对象没有属性'value'

AttributeError: 'int' object has no attribute 'isdigit'(python下的isdigit函数)

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

python出错:AttributeError: 'int' object has no attribute 'encode'

pyqt5 报错AttributeError: 'MyWindow' object has no attribute 'config_ui'?

python提示AttributeError: 'NoneType' object has no attribute 'append'