Error when trying to write unicode xml to zipfile

Stefan Behnel stefan.behnel-n05pAM at web.de
Tue Jul 10 03:25:30 EDT 2007


Gabriel Genellina wrote:
> En Mon, 09 Jul 2007 20:11:24 -0300, Martin <martin.clausen at gmail.com>
> escribió:
> 
>> I get below error when trying to write unicode xml to a zipfile.
>>
>>     zip.writestr('content.xml', content.toxml())
>>   File "/usr/lib/python2.4/zipfile.py", line 460, in writestr
>>     zinfo.CRC = binascii.crc32(bytes)       # CRC-32 checksum
>> UnicodeEncodeError: 'ascii' codec can't encode character u'\u25cf' in
>> position 2848: ordinal not in range(128)
>>
>> Any ideas?
> 
> Encode before writing. Assuming you want to use utf-8:
> zip.writestr('content.xml', content.toxml().encode('utf-8'))

Unless, obviously, you were serialising to a non-utf8 encoding. But since the
"toxml()" method seems to return unicode here (which sounds surprising), I
expect it a) to provide no XML declaration at all or b) to be broken anyway.

Stefan



More information about the Python-list mailing list