UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\xe1′ in position 8: ordinal not in range(128)

If you do the following:

[str(x) for x in data]

You’ll sometimes get this error:

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

This indicates that you have non-ascii characters in the data, and should use a wider type. You can fix it by doing the following (alternately, you can do something like base64 encoding, although I’m not sure why you’d want to do that):

[unicode(x) for x in data]

Interested in Python? I send out weekly, personalized emails with articles and conference talks. Click here to see an example and subscribe.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *