Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting XML to file corrupts files in python
    primarykey
    data
    text
    <p>I'm attempting to write contents from <code>xml.dom.minidom</code> object to file. The simple idea is to use 'writexml' method:</p> <pre><code>import codecs def write_xml_native(): # Building DOM from XML xmldoc = minidom.parse('semio2.xml') f = codecs.open('codified.xml', mode='w', encoding='utf-8') # Using native writexml() method to write xmldoc.writexml(f, encoding="utf=8") f.close() </code></pre> <p>The problem is that it corrupts the non-latin-encoded text in the file. The other way is to get the text string and write it to file explicitly:</p> <pre><code>def write_xml(): # Building DOM from XML xmldoc = minidom.parse('semio2.xml') # Opening file for writing UTF-8, which is XML's default encoding f = codecs.open('codified3.xml', mode='w', encoding='utf-8') # Writing XML in UTF-8 encoding, as recommended in the documentation f.write(xmldoc.toxml("utf-8")) f.close() </code></pre> <p>This results in the following error:</p> <pre><code>Traceback (most recent call last): File "D:\Projects\Semio\semioparser.py", line 45, in &lt;module&gt; write_xml() File "D:\Projects\Semio\semioparser.py", line 42, in write_xml f.write(xmldoc.toxml(encoding="utf-8")) File "C:\Python26\lib\codecs.py", line 686, in write return self.writer.write(data) File "C:\Python26\lib\codecs.py", line 351, in write data, consumed = self.encode(object, self.errors) UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 2064: ordinal not in range(128) </code></pre> <p>How do I write an XML text to file? What is it I'm missing?</p> <p><strong>EDIT</strong>. Error is fixed by adding decode statement: <code>f.write(xmldoc.toxml("utf-8").decode("utf-8"))</code> But russian symbols are still corrupted.</p> <p>The text is not corrupted when viewed in an interpreter, but when it's written in file.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload