我找到了一個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