473,387 Members | 3,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

minidom's setAttribute + UnicodeDecodeError

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(128)
------------------------------------------------------------

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

Jul 18 '05 #1
1 4862
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
Jul 18 '05 #2

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

Similar topics

1
by: Roman Yakovenko | last post by:
Hi. What I don't understand is why I dump to xml string that contains new - lines and when I read it from xml new lines replaced by spaces. May be I do something wrong? Or may be I just need to...
4
by: Derek Basch | last post by:
Hello All, I ran into a problem while dynamically constructing XHTML documents using minidom. If you create a script tag such as: script_node_0 = self.doc.createElement("script")...
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
2
by: Peter Møllerud | last post by:
I'm very new to Python, so consider this a silly newbie question... Anyway, I'm building a small application to generate a XML. Using document from minidom, I'm doing something like : from...
5
by: fscked | last post by:
Hi guys/gals. I am trying to write and xml file from data parsed from a csv. I can get everything to work except that I cannot get minidom to do --> ö which needless to say is driving me nuts....
6
by: Dan | last post by:
I'm using python's xml.dom.minidom module to generate xml files, and I'm running into memory problems. The xml files I'm trying to create are relatively flat, with one root node which may have...
1
by: Paul Kozik | last post by:
I am trying to write a script that reads an XML file (using the minidom module), makes an edit to a few attributes, then saves it back. If I use minidom.Document() to create the xml file, then...
2
by: JYA | last post by:
Hi. I was writing an xmltv parser using python when I faced some weirdness that I couldn't explain. What I'm doing, is read an xml file, create another dom object and copy the element from...
2
by: ashmir.d | last post by:
Hi, I am trying to parse an xml file using the minidom parser. <code> from xml.dom import minidom xmlfilename = "sample.xml" xmldoc = minidom.parse(xmlfilename) </code> The parser is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.