我有数据要放入带有python 2.7的xml文件中,但我有这个编码错误:
File "script.py", line 121, in buildXML
output_file.write( prettify(tickets))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 2621: ordinal not in range(128)
我的代码:
output_file = open( 'tickets.xml', 'w' )
output_file.write( '<?xml version="1.0"?>' )
output_file.write( prettify(tickets))
output_file.close()
我在互联网上尝试了一些没有成功的解决方案
答案 0 :(得分:0)
output_file = open('tickets.xml', 'w')
output_file.write('<?xml version="1.0"?>')
output_file.write(prettify(unicode('{}'.format(tickets), errors='ignore').encode('ascii', 'ignore'))
output_file.close()
我希望这可以解决encode
错误