Ubuntu Debian中python3的解码错误(UnicodeEncodeError:';ascii';编解码器可以';不在适当位置编码字符)
我在Python3脚本中有解码问题。我知道这个问题已经讨论过了,但我找到的解决办法没有帮助 问题:我有“Unicode字符串”类型的xml(从doc.toxml()获取),它会写入文件。我响应错误“UnicodeEncodeError:“ascii”编解码器无法对位置723-727处的字符进行编码:序号不在范围(128)”内。如果我编码doc.toxml(encoding='UTF-8')和解码xml.decode('UTF-8'),则会出现相同的错误 片段1:Ubuntu Debian中python3的解码错误(UnicodeEncodeError:';ascii';编解码器可以';不在适当位置编码字符),ubuntu,python-3.x,debian,Ubuntu,Python 3.x,Debian,我在Python3脚本中有解码问题。我知道这个问题已经讨论过了,但我找到的解决办法没有帮助 问题:我有“Unicode字符串”类型的xml(从doc.toxml()获取),它会写入文件。我响应错误“UnicodeEncodeError:“ascii”编解码器无法对位置723-727处的字符进行编码:序号不在范围(128)”内。如果我编码doc.toxml(encoding='UTF-8')和解码xml.decode('UTF-8'),则会出现相同的错误 片段1: resXmlFormat
resXmlFormat = doc.toxml()
doc = '<?xml version="1.0" ?><ROOT><param>123</param></ROOT>'
file = open('/tmp/test.xml', 'w+')
file.write(resXmlFormat)
file.close()
片段2(已编码):
在我本地的Ubuntu上,没关系。但是在Debian服务器上是错误的。在Ubuntu错误中使用LC_ALL=C运行。它如何影响区域设置?一切正常。需要在open函数中添加编码或在byte视图中打开。
file.write(resXmlFormat)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 723-727: ordinal not in range(128)
resXmlFormat = doc.toxml(encoded='UTF-8')
doc = '<?xml version="1.0" encoding="UTF-8"?><ROOT><param>123</param></ROOT>'
file = open('/tmp/test.xml', 'w+')
file.write(resXmlFormat.decode('utf-8'))
file.close()
file.write(resXmlFormat.decode('utf-8'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 723-727: ordinal not in range(128)