15

I'm having some issues with this, I keep getting:

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.

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()
5
  • 2
    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 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]] Commented Oct 2, 2015 at 3:55
  • @Pramod you should post an answer with your solution, which looks perfectly valid to me, too. Commented Oct 2, 2015 at 3:57
  • @Pramod Thanks for the tip. I'm now getting another error here: SyntaxError: Non-ASCII character '\xe2' in file SubscribersPullCrossPlatform.py on line 49, but no encoding declared; Commented Oct 2, 2015 at 3:59
  • @Waveformer put # -*- coding: utf-8 -*- at the top of your file then. Commented Oct 2, 2015 at 4:04
  • @Pramod Thanks for helping out here. After adding to the header, I now get UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128) Commented Oct 2, 2015 at 4:09

3 Answers 3

11

Use this :

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

instead of :

s.encode('utf-8')
Sign up to request clarification or add additional context in comments.

Comments

7

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

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

4 Comments

There still seems to be some issues. I'm finding encoding to be a sensitive area when running scripts like this one.
@Waveformer Now this is different issue, to debug this problem I have to simulate your problem in my side. You have to look into type of character in your string where you are performing encoding operation. But make sure while doing converstion you don't get any noise.
interestingly enough it managed to do one before throwing the error: 1,Y30JRSgfhYXA6i6xX1erWg,Smosh,6947355,1806850,412974 Traceback (most recent call last): File "SubscribersPullCrossPlatform.py", line 51, in <module> row = [str(s).encode('utf-8') for s in [userID,titleResponse,facebooksubscribers,instagramsubscribers,vinesubscribers]] UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128) (venv)Maxs-MacBook-Pro:Leaderboard Scripts maxramsay$
If you are using Python 2.7 copy the code for UTF8Recoder and UnicodeWriter from docs.python.org/2.7/library/csv.html. It uses unicode(s, "utf-8")
1

I had a similar issue when loading data from an xlsx file. The problem I ran into after implementing the solutions above was that I would receive the error:

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

since the data I was parsing was not just unicode. The solution I found was a simple try/except where I only .encode('utf-8') if an error gets thrown. Here is the code:

    try:
        s2 = str(foo)
    except:
        s2 = foo.encode('utf-8').strip()

I don't know if this was a one off thing or if other people might be having this issue. I hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.