Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > minidom's setAttribute + UnicodeDecodeError

Reply
Thread Tools

minidom's setAttribute + UnicodeDecodeError

 
 
Ruslan
Guest
Posts: n/a
 
      09-07-2004
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

.....
node.setAttribute('style:name', _encode(value))
.....

i get UnicodeDecodeError:
------------------------------------------------------------
Traceback (most recent call last):
File "stnreplace.py", line 107, in ?
StylesHelper(fname).replace(trdict)
File "stnreplace.py", line 63, in replace
node.setAttribute('style:name', _encode(uval))
File "/usr/local/lib/python2.3/site-packages/_xmlplus/dom/minidom.py",
line 704, in setAttribute
elif value != attr.value:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 14:
ordinal not in range(12
------------------------------------------------------------

The value passed to setAttribute is utf8 string, i tried with different
encodings, but no luck.
Could somebody give any suggestion how to solve that? Seems problem is
in 'ascii' codec in minidom, but how to make it handle not just ascii?
TIA

P.S. Tested with python2.3.3 & python2.3.4

Best regards,
Ruslan

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      09-07-2004
Ruslan wrote:
> def _encode(v):
> if isinstance(v, UnicodeType):
> v = v.encode(v)
> return v
>
> ....
> node.setAttribute('style:name', _encode(value))
> ....

[...]
> Could somebody give any suggestion how to solve that? Seems problem is
> in 'ascii' codec in minidom, but how to make it handle not just ascii?


The problem is in your code. node.setAttribute requires both the
attribute name and the attribute value to be Unicode objects, as
per the DOM spec.

For backwards-compatibility, ease-of-use, and performance reasons,
it does not actually check that these are Unicode objects, and it
will work with byte strings just fine as long as they are ASCII.
But this would still be an error in the application, which really
needs to pass Unicode objects.

IOW: just remove the _encode call, and all will be fine.

Regards,
Martin
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UnicodeDecodeError ash Python 5 12-02-2005 07:33 AM
[pysqlite 2.0.2] UnicodeDecodeError: 'utf8' codec can't decode bytes in position 44-45: invalid data F. GEIGER Python 0 05-27-2005 07:29 PM
UnicodeDecodeError Thomas Thomas Python 2 05-05-2005 02:48 AM
request.setAttribute(...) versus session.setAttribute(...) Matt Java 7 11-08-2004 02:00 PM
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 10: ordinal not in range(128) Robin Siebler Python 4 10-08-2004 08:03 PM



Advertisments