Python 返回 AttributeError: 'int' 对象没有属性 'encode'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32900305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning AttributeError: 'int' object has no attribute 'encode'
提问by Waveformer
I'm having some issues with this, I keep getting:
我遇到了一些问题,我不断收到:
AttributeError: 'int' object has no attribute 'encode'
AttributeError: 'int' object has no attribute 'encode'
When I run it.
当我运行它时。
I thought UTF-8 would be the go to for this. Subscribers will only ever return numbers, or NoneTypes.
我认为 UTF-8 将是这个目标。订阅者只会返回数字或 NoneTypes。
Any help would be greatly appreciated.
任何帮助将不胜感激。
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()
回答by Pramod
because one of these
因为其中之一
[userID,titleResponse,facebooksubscribers,instagramsubscribers,vinesubscribers]?
element is int and you can't perform encode operation on int. You may want to do the type casting in your for loop. Replace
element 是 int 并且您不能对 int 执行编码操作。您可能希望在 for 循环中进行类型转换。代替
row = [s.encode('utf-8') for s in [userID,titleResponse,facebooksubscribers,instagramsubscribers,vinesubscribers]]??
with
和
row = [str(s).encode('utf-8') for s in [userID,titleResponse,facebooksubscribers,instagramsubscribers,vinesubscribers]]?
?
?
回答by Mojtaba Arvin
Use this :
用这个 :
repr(s).encode('utf-8')
instead of :
代替 :
s.encode('utf-8')