UnicodeEncodeError while reading xml file (newbie question)

nikosk nikos.nikos.nikos.nikos at gmail.com
Sat Jun 7 20:12:04 EDT 2008


I just spent a whole day trying to read an xml file and I got stuck
with the following error:

Exception Type:  	UnicodeEncodeError
Exception Value: 	'charmap' codec can't encode characters in position
164-167: character maps to <undefined>
Exception Location:  	C:\Python25\lib\encodings\cp1252.py in encode,
line 12

The string that could not be encoded/decoded was: H_C="ΚΙΝΑ" A_C

After some tests I can say with confidence that the error comes up
when python finds those greek characters after H_C="

The code that reads the file goes like this :

from xml.etree import ElementTree as ET

def read_xml(request):
    data = open('live.xml', 'r').read()
    data = data.decode('utf-8', 'replace')
    data = ET.XML(data)

I've tried all the combinations of str.decode str.encode  I could
think of but nothing.

Can someone please help ?



More information about the Python-list mailing list