UnicodeDecodeError, how to elegantly deal with this?

Jorgen Bodde jorgen.maillist at gmail.com
Mon Aug 4 14:23:41 EDT 2008


Hi All,

I am relatively new to python unicode pains and I would like to have
some advice. I have this snippet of code:

def playFile(cmd, args):
    argstr = list()
    for arg in appcfg.options[appcfg.CFG_PLAYER_ARGS].split():
        thefile = args["file"]
        filemask = u"%file%"
        therep = arg.replace(filemask, thefile)       ##### error here
        argstr.append(therep)
    argstr.insert(0, appcfg.options[appcfg.CFG_PLAYER_PATH])

    try:
        subprocess.Popen( argstr )
    except OSError:
        cmd.html = "<h1>Can't play file</h1></br>" + args["file"]
        return

    cmd.redirect = _getBaseURL("series?cmd_get_series=%i" % args["id"])
    cmd.html = ""

-------------------

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

Can anybody guide me to an elegant solution?

Thanks in advance!
- Jorgen



More information about the Python-list mailing list