1

I am using Google App Engine for Python, but I get a unicode error is there a way to work around it? Here is my code:

def get(self):
    contents = db.GqlQuery("SELECT * FROM Content ORDER BY created DESC")
    output = StringIO.StringIO()
    with zipfile.ZipFile(output, 'w') as myzip:
        for content in contents:
            if content.code:
                code=content.code
            else:
                code=content.code2
            myzip.writestr("udacity_code", code)

    self.response.headers["Content-Type"] = "application/zip"
    self.response.headers['Content-Disposition'] = "attachment; filename=test.zip"
    self.response.out.write(output.getvalue())

I now get a unicode error:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xf7 in position 12: ordinal not in range(128)

I believe it is coming from output.getvalue()... Is there a way to fix this?

dda
  • 6,030
  • 2
  • 25
  • 34
areke
  • 1,093
  • 1
  • 14
  • 23
  • If you include the complete stacktrace, we won't have to guess as to where the error occurs - the stacktrace tells you. – Nick Johnson Jul 24 '12 at 07:15

3 Answers3

2

@Areke Ignacio's answer is the fix. For a brief walkthrough here is a post I did recently "Python and Unicode Punjabi" https://www.pippallabs.com/blog/python-and-unicode-panjabi

PunjCoder
  • 450
  • 1
  • 4
  • 12
0

I had the exact same issue. in the end I solved it by changing the call to writestr from

myzip.writestr("udacity_code", code)

to

myzip.writestr("udacity_code", code.encode('utf-8'))
elewinso
  • 2,453
  • 5
  • 22
  • 27
-1

From this link:

Python UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 ordinal not in range(128)

However in the meantime your problem is that your templates are ASCII but your data is not (can't tell if it's utf-8 or unicode). Easy solution is to prefix each template string with u to make it Unicode.

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190