UnicodeDecodeError, how to elegantly deal with this?

Edwin.Madari at VerizonWireless.com Edwin.Madari at VerizonWireless.com
Mon Aug 4 14:46:23 EDT 2008


if you can print out values  of 'filemask', and 'thefile' variables, when it crashes, I can help.

thx. Edwin

-----Original Message-----
From: python-list-bounces+edwin.madari=verizonwireless.com at python.org
[mailto:python-list-bounces+edwin.madari=verizonwireless.com at python.org]
On Behalf Of Jorgen Bodde
Sent: Monday, August 04, 2008 2:24 PM
To: python-list at python.org
Subject: UnicodeDecodeError, how to elegantly deal with this?


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
--
http://mail.python.org/mailman/listinfo/python-list



The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure.  If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof.  Thank you.





More information about the Python-list mailing list