minidom's setAttribute + UnicodeDecodeError

"Martin v. Löwis" martin at
Tue Sep 7 22:33:05 CEST 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.


More information about the Python-list mailing list