1

I'm have some problems with Google APIs Client Library for Python. I can authenticate, create a drive instance and, for now, I just ask a list of all the files that I have in my drive:

drive_service = build('drive', 'v2', http_auth)
files = drive_service.files().list().execute()

This give me a dictionary, but I need a list. Then, I do:

result = []
result.extend(files['items'])
for item in files['items']:
  print item['title']
  print list(item['title'])
  print type(item['title'])

and the out put is:

fotos.zip
[u'f', u'o', u't', u'o', u's', u'.', u'z', u'i', u'p']
<type 'unicode'>

But I need the list like this: [fotos.zip]. I guess the problem is in the unicode, but if I add this line at the first of script:

# -*- coding: utf-8 -*-
# code 
print list(str(item['title']))

I have this error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 6: ordinal not in range(128)

What's I can Do? Thanks!

F.N.B
  • 1,539
  • 6
  • 23
  • 39
  • These two links might be helpful: https://pythonadventures.wordpress.com/tag/unicodeencodeerror/ and http://stackoverflow.com/questions/8590912/python-throws-unicodeencodeerror-although-i-am-doing-str-decode-why Hope that helps! – KRR Jan 27 '15 at 19:05

1 Answers1

0

The error comes from using str to convert your unicode string, which means that it goes from unicode->ascii->utf-8, and since ascii doesn't support those characters it raises an exception. What you need to do is use the encode() and decode() functions.

To have the result you need, try this:

print list(item['title'].encode('UTF-8'))

also, you can use 'ignore', 'replace' to either ignore or replace the special characters.

MayK
  • 1,269
  • 1
  • 10
  • 24