代码:
>>> from django.core import serializers
>>> objects = serializers.deserialize('xml', fixturestr)
>>> o = next(objects)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/marcintustin/oneclickrep/oneclickcosvirt/lib/python2.7/site-packages/django/core/serializers/xml_serializer.py", line 156, in next
for event, node in self.event_stream:
File "/usr/lib/python2.7/xml/dom/pulldom.py", line 232, in next
rc = self.getEvent()
File "/usr/lib/python2.7/xml/dom/pulldom.py", line 265, in getEvent
self.parser.feed(buf)
File "/usr/lib/python2.7/xml/sax/expatreader.py", line 210, in feed
self._parser.Parse(data, isFinal)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xc5' in position 565: ordinal not in range(128)
fixturestr
是unicode对象。消除非ascii字符不是一个选项。如何阻止反序列化程序执行此操作?在
尝试使用smart_str将unicode对象转换为字符串:
似乎如果传递了一个unicode对象,反序列化程序将尝试将其编码为unicode。为了避免这种情况,必须通过一个bytestring:
相关问题 更多 >
编程相关推荐