python - 统一码编码错误 : 'latin-1' codec can't encode character u'\u03c7' in position 66: ordinal not in range(256)

标签 python unicode flask

我有时会在我的网络服务器上遇到此错误。

Traceback (most recent call last):
File "/bin/user_wsgi_wrapper.py", line 130, in __call__
    self.error_log_file.logger.exception("Error running WSGI application")
File "/usr/lib/python2.7/logging/__init__.py", line 1185, in exception
    self.error(msg, *args, **kwargs)
File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
    self._log(ERROR, msg, args, **kwargs)
File "/usr/lib/python2.7/logging/__init__.py", line 1270, in _log
    record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
File "/usr/lib/python2.7/logging/__init__.py", line 1244, in makeRecord
    rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
    self.threadName = threading.current_thread().name
File "/usr/lib/python2.7/threading.py", line 1160, in currentThread
    return _active[_get_ident()]
File "/bin/user_wsgi_wrapper.py", line 122, in __call__
    app_iterator = self.app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1690, in wsgi_app
    return response(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 1082, in __call__
    app_iter, status, headers = self.get_wsgi_response(environ)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 1072, in get_wsgi_response
    return app_iter, self.status, headers.to_list()
File "/usr/local/lib/python2.7/dist-packages/werkzeug/datastructures.py", line 1141, in to_list
    for k, v in self]
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u03c7' in position 66: ordinal not in range(256)

问题是我不知道代码中哪里会发生这种情况,所以我不知道应该将 .encode('utf-8', 'ignore') 放在哪里。

最佳答案

您似乎正在尝试设置一个包含不在 ISO-8859-1 字符集中的字符的 header 。目前,HTTP header 只能包含来自该编解码器的字符。 Werkzeug 的更新版本需要 Latin-1 编解码器,而不是更严格地遵循 WSGI 规范。

See this Flask issue for a message about this from the maintainer.它提到在RFC 5987 中描述了使用范围外字符的正确方法。 ,可能会在未来实现。

关于python - 统一码编码错误 : 'latin-1' codec can't encode character u'\u03c7' in position 66: ordinal not in range(256),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29164105/

相关文章:

python - 使用 .format : Python 3 在循环输出之间添加一个空格

java - 在 Java 中生成 3 字节(0x800 到 0xffff)UTF-8 编码

python - 在 Flask 的应用上下文中保持 py2neo 连接

json - 从字符串中删除无效的 UTF-8 字符

stored-procedures - Oracle 存储过程中的 Unicode

json - 使用 json.dumps 让 Python Flask 返回 application/json

python - 提供位于 Flask 蓝图文件夹下的静态文件

python - 在 GitHub Pages 上显示交互式绘图图表(.html 文件)

python - 我可以通过 Python 控制 powershell "session"

python - Tornado 。类似 Django 的测试运行器和测试数据库