Skip to content

Dealing with Python 2.x UnicodeDecodeErrors

2011 January 4
by Tedb0t

Tired of seeing this?

<type 'exceptions.UnicodeDecodeError'> at /such/and/such
'ascii' codec can't decode byte 0xf8 in position 12: ordinal not in range(128)

So am I.  I had a bytestring with extended characters in it:

data = '\xfcnic\xf8de test'

And no matter what I tried:

data.decode('utf-8')

…I kept getting errors. Turns out those extended characters simply aren’t in the utf-8 codec, so I tried latin-1:

data.decode('latin-1')

And it works! At long last!!

Related Posts:


No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS