gpt4 book ai didi

python - 'ascii' 编解码器无法解码位置 5367 : ordinal not in range(128) 中的字节 0xe2

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

  • Django 1.6.1
  • python 3.2.3
  • Apache 2.2.2
  • Ubuntu 12.04
  • Postgres 9.1.11

我在尝试导入序列化 XML 文件时遇到此错误。我从我用于开发服务器的一个数据库导出它,片刻之后我将它导入另一个开发服务器(在同一台机器上)上的另一个数据库。我很困惑为什么它不处理导入时的 UTF-8 字符。我没有做任何特别的事情。另外,在我上次测试时,相同的代码也有效。这是我在应用程序的 views.py...

中用于导出和导入的代码
from django.shortcuts import render, render_to_response, get_object_or_404
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib.auth.decorators import user_passes_test
from django.core import serializers

@user_passes_test(lambda u: u.is_superuser)
def export_serial(request):
'''
This exports data using Django serializing.
See: https://docs.djangoproject.com/en/dev/topics/serialization/
'''
XMLSerializer = serializers.get_serializer("xml")
xml_serializer = XMLSerializer()
xml_serializer.serialize(list(ThisModel.objects.all())
+ list(ThatModel.objects.all())
+ list(ThirdModel.objects.all()))
data = xml_serializer.getvalue()

response = HttpResponse(mimetype='text/xml')
response['Content-Disposition'] = 'attachment; filename=my-backup.xml'
response.write(data)
return response


@user_passes_test(lambda u: u.is_superuser)
def import_serial(request):
'''
This is for importing data that was saved/backed up, as serialized.
'''
xmlfile = '/home/zamphatta/path/to/my-backup.xml'
serialfile = open(xmlfile,'r')
data = serialfile.read()
serialfile.close()

for deserialized_object in serializers.deserialize("xml", data):
deserialized_object.save()

template = loader.get_template('z-style/index.htm')
context = RequestContext(request, {
'title': data,
})
return HttpResponse(template.render(context))

我用 Google 搜索并检查了所有相关的 StackOverflow 问题(但答案并没有给我提供答案),但仍然不知道为什么会抛出这个错误。我所知道的是非 ascii 字符使它呕吐,这没有意义,因为 Python 对 UTF-8 字符没有问题,而且我没有做任何会导致代码突然以不同方式对待它的奇怪事情.

更新:这是错误消息的完整版本...

UnicodeDecodeError at /sdb/import/

'ascii' codec can't decode byte 0xe2 in position 5367: ordinal not in range(128)

Request Method: GET

Request URL: http://coral/sdb/import/

Django Version: 1.6.1

Exception Type: UnicodeDecodeError

Exception Value:
'ascii' codec can't decode byte 0xe2 in position 5367: ordinal not in range(128)

Exception Location: /usr/lib/python3.2/encodings/ascii.py in decode, line 26

Python Executable: /usr/bin/python

Python Version: 3.2.3

Python Path:
['/usr/local/lib/python3.2/dist-packages/distribute-0.6.49-py3.2.egg',
'/usr/local/lib/python3.2/dist-packages/CherryPy-3.2.4-py3.2.egg',
'/usr/lib/python3.2',
'/usr/lib/python3.2/plat-linux2',
'/usr/lib/python3.2/lib-dynload',
'/usr/local/lib/python3.2/dist-packages',
'/usr/local/lib/python3.2/dist-packages/setuptools-0.6c11-py3.2.egg-info',
'/usr/lib/python3/dist-packages',
'/home/dfy/code/zamphatta/',
'/home/dfy/code/zamphatta/zamphatta/']

最佳答案

似乎当服务器启动 python3 manage.py runserver 时,导入工作正常。当 Django 通过 wsgi 在 Apache 上运行时,当我通过 Django 导入它时它不起作用。所以我猜 Apache 以某种方式干扰了它。它现在可以工作,只要我直接通过 Django 导入。

关于python - 'ascii' 编解码器无法解码位置 5367 : ordinal not in range(128) 中的字节 0xe2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21088975/

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