UnicodeDecodeError, how to elegantly deal with this?

Jorgen Bodde jorgen.maillist at gmail.com
Tue Aug 5 04:25:30 EDT 2008


Hi Edwin,

Filemask is obvious as it is assigned in the python code itself. It is
"%file%". The idea is that the file clicked is substituted for the
"%file%" by the replace action. The file that needs to be substituted
is a simple file on disk.

Here is a dump of the file and it's characters. I do understand that
it is not in the range of ASCII but how can I make it so that it will
work?

>>>E:\Series\Series\American Dad\American Dad - S03E15 - Stanny Slickers II, The Legend of OllieÂŽs Gold.avi

E (69): (58)\ (92)S (83)e (101)r (114)i (105)e (101)s (115)\ (92)S
(83)e (101)r (114)i (105)e (101)s (115)\ (92)A (65)m (109)e (101)r
(114)i (105)c (99)a (97)n (110)  (32)D (68)a (97)d (100)\ (92)A (65)m
(109)e (101)r (114)i (105)c (99)a (97)n (110)  (32)D (68)a (97)d (100)
 (32)- (45)  (32)S (83)0 (48)3 (51)E (69)1 (49)5 (53)  (32)- (45)
(32)S (83)t (116)a (97)n (110)n (110)y (121)  (32)S (83)l (108)i
(105)c (99)k (107)e (101)r (114)s (115)  (32)I (73)I (73), (44)  (32)T
(84)h (104)e (101)  (32)L (76)e (101)g (103)e (101)n (110)d (100)
(32)o (111)f (102)  (32)O (79)l (108)l (108)i (105)e (101)Â (194)Ž
(180)s (115)  (32)G (71)o (111)l (108)d (100). (46)a (97)v (118)i
(105)Traceback (most recent call last):

  File "D:\backup\important\src\airs\gui\AirsFrame.py", line 575, in
_onWebRequest

    webdispatch.execute(cmd, id, args)

  File "D:\backup\important\src\airs\webserver\webdispatch.py", line
167, in execute

    _cmd_dispatcher[cmd](cb, args)

  File "D:\backup\important\src\airs\webserver\webdispatch.py", line
122, in playFile

    therep = arg.replace(filemask, thefile)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position
93: ordinal not in range(128)

----

I assume it is about this part:

 OllieÂŽs Gold.avi

But what can I do to decode that properly?

With regards,
- Jorgen




On Mon, Aug 4, 2008 at 8:46 PM,  <Edwin.Madari at verizonwireless.com> wrote:
> 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.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list