【问题标题】:UnicodeDecodeError with Python 2.7 and suds 4.0 with German languageUnicodeDecodeError 与 Python 2.7 和 suds 4.0 与德语
【发布时间】:2016-10-02 05:23:31
【问题描述】:

我有一个 python-django 项目,我正在尝试将德语和意大利语保存在我的 Postgresql 数据库中,该数据库仅供该项目使用。我正在尝试通过我添加的 Html 表单发送我想要保存在数据库中的结果:

accept-charset="UTF-8" 

结果发送到我在顶部添加的views.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

我也尝试将 views.py 中的编码、解码为 utf-8、ISO-5589-1、latin-1,但它不起作用。我相信这个错误与这篇文章有关,但我不明白他们所说的错误已修复但仍然不适合我的解决方案。

https://fedorahosted.org/suds/ticket/361

这是我的回溯:

Environment:


Request Method: POST


Django Version: 1.4
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'ui')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.locale.LocaleMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/hatzimin/web/indivo_ui_server/apps/problems/views.py" in new_problem
  281.                                                content_type='application/xml')
File "/home/hatzimin/web/indivo_ui_server/indivo_client_py/client.py" in __call__
  154.         return func(self.url, *args, **kwargs)
File "/home/hatzimin/web/indivo_ui_server/indivo_client_py/client.py" in post
  68.         return self.request(self.api_base+uri, uri_params, method="POST", body=body, headers=headers)
File "/home/hatzimin/web/indivo_ui_server/indivo_client_py/client.py" in request
  132.         return super(IndivoClient, self).request(uri, *args, **kwargs)
File "/home/hatzimin/web/indivo_ui_server/indivo_client_py/oauth2/__init__.py" in request
  697.             connection_type=connection_type)
File "/home/hatzimin/web/indivo_ui_server/indivo_client_py/oauth2/httplib2/__init__.py" in request
  1544.                     (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/home/hatzimin/web/indivo_ui_server/indivo_client_py/oauth2/httplib2/__init__.py" in _request
  1294.         (response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/home/hatzimin/web/indivo_ui_server/indivo_client_py/oauth2/httplib2/__init__.py" in _conn_request
  1231.                 conn.request(method, request_uri, body, headers)
File "/usr/lib/python2.7/httplib.py" in request
  955.         self._send_request(method, url, body, headers)
File "/usr/lib/python2.7/httplib.py" in _send_request
  989.         self.endheaders(body)
File "/usr/lib/python2.7/httplib.py" in endheaders
  951.         self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py" in _send_output
  809.             msg += message_body

Exception Type: UnicodeDecodeError at /apps/problems/problems/new
Exception Value: 'ascii' codec can't decode byte 0xc3 in position 207: ordinal not in range(128)

【问题讨论】:

  • 我找到了解决方案,但我无法将其发布为答案,我被重定向到空白页。所以我在views.py中添加了这个:problem_xml= problem_xml.encode('ascii', 'xmlcharrefreplace') 然后程序正确完成。数据库中使用变音符号保存的德语字符。还使用希腊语和意大利语进行了测试

标签: python-2.7 unicode utf-8 ascii suds


【解决方案1】:

可能suds 正在返回以utf-8 编码的'str' 类型的字符串。在使用它之前,您应该将此字符串转换为类型'unicode'

在收到来自suds 的字符串后立即尝试在其上调用django.utils.encoding.smart_unicode

【讨论】:

  • 你知道我怎样才能找到在哪里添加这个吗?
  • @user1431148 这个函数应该在收到 suds 的结果后立即调用
猜你喜欢
  • 2014-02-22
  • 1970-01-01
  • 1970-01-01
  • 2012-06-07
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
  • 1970-01-01
  • 2014-08-11
相关资源
最近更新 更多