472,378 Members | 1,298 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

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\webdispatc h.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
Aug 4 '08 #1
3 6544
On Aug 5, 4:23 am, "Jorgen Bodde" <jorgen.maill...@gmail.comwrote:
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\webdispatc h.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
Aug 5 '08 #2
Hi John,
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".
Well I guess you misunderstood what I meant. I meant I am a simple
developer, getting a string from the file system that happens to be in
some kind of encoding. It is totally a mystery to me why it crashes on
that so that is what I meant with not wanted to be bothered with it,
because I don't see any obvious reason why, not that I am too lazy to
deal with it, it simply seems strange to me.
In this case, less is more; remove the u prefix in the line
filemask = u"%file%"
Ok thanks. I thought making it unicode because it is a search string
that is used in a UTF-8 encoded replacement, would solve it,
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
I never said that I have an attitude towards unicode, I simply
misunderstood it's inner workings. Thanks for the link I will look at
it.

ps. sorry for the direct mail, I can't get used to one mailinglist
always replying to the list, and the other replying to the user by
default ;-)

With regards,
- Jorgen

On Tue, Aug 5, 2008 at 11:00 AM, John Machin <sj******@lexicon.netwrote:
On Aug 5, 4:23 am, "Jorgen Bodde" <jorgen.maill...@gmail.comwrote:
>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\webdispat ch.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
--
http://mail.python.org/mailman/listinfo/python-list
Aug 5 '08 #3
On Aug 5, 8:37 pm, "Jorgen Bodde" <jorgen.maill...@gmail.comwrote:
Hi John,
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".

Well I guess you misunderstood what I meant.
Sorry, it's my ETL (English as a Third Language) problem; my mother
tongue is the Queensland dialect of Australian :-)
>
In this case, less is more; remove the u prefix in the line
filemask = u"%file%"

Ok thanks. I thought making it unicode because it is a search string
that is used in a UTF-8 encoded replacement, would solve it,
"UTF-8 encoded" implies a str (8-bits per character) object, not a
unicode object. Solve what? What problem did you have before you put
the u in there?
>
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

I never said that I have an attitude towards unicode, I simply
misunderstood it's inner workings.
I must have misunderstood "pains" and "bother", eh?

Cheers,
John
Aug 5 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ruslan | last post by:
Hi, everybody. In this excerpt of code enc = 'some_type_of_encoding' def _encode(v): if isinstance(v, UnicodeType): v = v.encode(v) return v
4
by: Robin Siebler | last post by:
I have no idea what is causing this error, or how to fix it. The full error is: Traceback (most recent call last): File "D:\ScriptRuntime\PS\Automation\Handlers\SCMTestToolResourceToolsBAT.py",...
2
by: dbri.tcc | last post by:
Hello I am getting somewhat random UnicodeDecodeError messages in my program. It is random in that I will be going through a pysqlite database of records, manipulate the results, and it will...
4
by: Robin Haswell | last post by:
Okay I'm getting really frustrated with Python's Unicode handling, I'm trying everything I can think of an I can't escape Unicode(En|De)codeError no matter what I try. Could someone explain to...
7
by: kath | last post by:
I have a number of excel files. In each file DATE is represented by different name. I want to read the date from those different file. Also the date is in different column in different file. To...
1
by: Karl | last post by:
error msg: Mod_python error: "PythonHandler mod_python.publisher" Traceback (most recent call last): File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 299, in HandlerDispatch...
4
by: Oleg Parashchenko | last post by:
Hello, I'm working on an unicode-aware application. I like to use "print" to debug programs, but in this case it was nightmare. The most popular result of "print" was: UnicodeDecodeError:...
0
by: Jorgen Bodde | last post by:
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...
3
by: Gilles Ganault | last post by:
Hello I'm getting this error while downloading and parsing web pages: ===== title = m.group(1) UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 48: ordinal not in...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.