UnicodeEncodeError while writing data to XML file

My goal is to write an XML file with multiple tags, the values ​​of which are in the regional language. I am using Python for this and using IDLE (Pythong GUI) for programming.

While I am trying to write words in the xmls file, it gives the following error:

UnicodeEncodeError: 'ascii' codec cannot encode characters at position 0-4: ordinal not in range (128)

I am not currently using the xml writer library; instead, I open the "test.xml" file and write data to it. This error occurs on the line: f.write(data)

If I replace the above write statement with a print statement, it prints the data correctly in the Python shell.

I am reading data from an Excel file that is not in UTF-8, 16 or 32 encoding formats. It's in a different format. cp1252 reads data correctly.

Any help getting this data written into an XML file would be much appreciated.

+2


a source to share


1 answer


You should be getting .decode

your input cp1252

to get Unicode strings and .encode

them in utf-8

(by far the preferred encoding for XML) at the time of writing, i.e.

f.write(unicodedata.encode('utf-8'))

      

where is unicodedata

obtained .decode('cp1252')

with incoming bytes.



It is possible to put lipstick on it using the codecs

Python standard library module to open the input and output files each with their correct encodings instead of the simple one open

, but what I am showing is the basic mechanism (and this is often, though not always, clearer and more explicit to apply it directly and not indirectly through codecs

- a matter of style and taste).

What does - general principle: convert your input strings to unicode as soon as you can immediately after receiving them, use unicode while processing, convert them back to byte strings at the end you can just before release them. It gives you the simplest, most direct life! -)

+6


a source