Python3 UnicodeDecodeError [英] Python3 UnicodeDecodeError
问题描述
我有一个运行Python 3的服务器上运行的Django 1.8应用,在记录和打印带有特殊字符的字符串时出现UnicodeDecodeError。
I have a Django 1.8 app running on a server with Python 3 and I get a UnicodeDecodeError when logging and printing strings with special characters.
>:python --version
python 3.4.3
例如,如果我尝试在外壳程序:
For example, if I try to run a silly method in the shell:
def print_test():
print('Test: èè') # any 'special char' like ä ç é û...
我得到堆栈跟踪:
>>> print_test()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/sailingadmin/sailing-admin/utest.py", line 2, in print_test
print('This is a test: \xe8\xe8')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 16-17: ordinal not in range(128)
与 print(u'Test:èè')
为什么会发生此错误?
utest.py是用utf-8编码的(源文件使用Python 3默认值)
utest.py is encoded in utf-8 (Python 3 default for source files)
>:echo $ LC_CTYPE
UTF-8
所有日志记录和打印都会引发UnicodeEncodeError ...
All logging and printing raises a UnicodeEncodeError...
推荐答案
LC_CTYPE格式如下: en_US.UTF-8
(点后的字符映射)
然后python将其用作日志记录和io的默认编码。
LC_CTYPE format as follows: en_US.UTF-8
(char map after the dot)
Then python uses it as default encoding for logging and io.
这篇关于Python3 UnicodeDecodeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!