我在带有一些西里尔文项目的 docker 容器中运行 python 3.6 + gunicorn + django 2.0.5,这就是我尝试使用 Django 在控制台中记录西里尔文字符串时看到的结果。
'ascii' codec can't encode character '\u0410' in position 0: ordinal not in range(128)
这也是 shell 中发生的事情
Python 3.6.5 (default, May 3 2018, 10:08:28)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> :�ириллица
同时,当我在 docker 容器外运行 python 3.5 时,一切正常:
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> Кириллица
有什么想法可以使 docker 中的 python 3.6 使用西里尔字符串正常工作吗?
最佳答案
在您的 Python 代码的第一行使用 # -*- coding: utf-8 -*-
。
并在您的 Dockerfile
中添加:
环境 PYTHONIOENCODING=utf-8
关于python - 再次出现UnicodeEncodeError(ascii codec无法编码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51424728/