gpt4 book ai didi

python - django 反序列化尝试将输入编码为 ascii?

转载 作者:数据小太阳 更新时间:2023-10-29 02:35:43 25 4
gpt4 key购买 nike

代码:

>>> 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 对象转换为字符串:

from django.utils.encoding import smart_str
from django.core import serializers

objects = serializers.deserialize('xml', smart_str(fixturestr))
o = next(objects)

关于python - django 反序列化尝试将输入编码为 ascii?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19669522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com