我找到了一个Python程序:Export Django database to xml file,它将django模型转换为xml表示形式。试图运行程序时出现这些错误。我的模型包含一些用法语写的文字。尝试将Django模型转换为XML时出现UnicodeEncodeError
Traceback (most recent call last):
File "xml_export.py", line 71, in <module>
writer.content(value)
File "xml_export.py", line 41, in content
self.output += str(text)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 3:
ordinal not in range(128)
如果我这样做,我得到:回溯(最后最近一次调用): 文件 “xml_export.py”,71行,在 writer.content(值) 文件 “xml_export.py” 41行,在内容 self.output + = str(text.encode(“utf-8”)) AttributeError:'int'object has no attribute'encode' –
Seitaridis
2011-01-27 14:06:37
这很奇怪。这意味着`text`包含一个整型变量。这反过来使得很难弄清楚为什么在`text`中会出现'é`,除非该变量沿着路径改变其类型(这在Python中是合法的),但是这使得这种情况更加困难。您需要提供更多信息 - “文本”来自哪里?你在做什么? – 2011-01-27 14:09:35