UnicodeDecodeError, how to elegantly deal with this?
John Machin
sjmachin at lexicon.net
Tue Aug 5 05:00:31 EDT 2008
On Aug 5, 4:23 am, "Jorgen Bodde" <jorgen.maill... at gmail.com> wrote:
> Hi All,
>
> I am relatively new to python unicode pains and I would like to have
> some advice. I have this snippet of code:
> thefile = args["file"]
> filemask = u"%file%"
> therep = arg.replace(filemask, thefile) ##### error here
> It crashes on this:
>
> 20:03:49: File
> "D:\backup\important\src\airs\webserver\webdispatch.py", line 117, in
> playFile therep = arg.replace(filemask, thefile)
>
> 20:03:49: UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in
> position 93: ordinal not in range(128)
>
> 20:03:49: Unhandled Error: <type 'exceptions.UnicodeDecodeError'>:
> 'ascii' codec can't decode byte 0xc2 in position 93: ordinal not in
> range(128)
>
> It chokes on a ` character in a file name. I read this file from disk,
> and I would like to play it. However in the replace action it cannot
> translate this character. How can I transparently deal with this issue
> because in my eyes it is simply replacing a string with a string, and
> I do not want to be bothered with unicode problems. I am not sure in
> which encoding it is in, but I am not experienced enough to see how I
> can solve this
If you don't want to be bothered with "unicode problems":
(1) Don't create a "unicode problem" when one doesn't exist.
(2) Don't bother other people with *your* "unicode problems".
>
> Can anybody guide me to an elegant solution?
>
Short path:
In this case, less is more; remove the u prefix in the line
filemask = u"%file%"
Long Path:
Ignorance is not bliss. Lose the attitude. Unicode is your friend, not
an instrument of Satan. Read this:
http://www.amk.ca/python/howto/unicode
By the way, how one's filesystem encodes file names can be a good
thing to know; in your case it appears to be UTF-8.
HTH,
John
More information about the Python-list
mailing list