gpt4 book ai didi

Python3 Unicode解码错误

转载 作者:行者123 更新时间:2023-12-01 04:18:57 24 4
gpt4 key购买 nike

我有一个 Django 1.8 应用程序在使用 Python 3 的服务器上运行,在记录和打印带有特殊字符的字符串时出现 UnicodeDecodeError。

>:python --版本
python 3.4.3

例如,如果我尝试在 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 默认值)

>:echo $LC_CTYPE
UTF-8

所有日志记录和打印都会引发 UnicodeEncodeError...

最佳答案

LC_CTYPE格式如下:en_US.UTF-8(点后的字符映射)然后 python 使用它作为日志记录和 io 的默认编码。

关于Python3 Unicode解码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33955276/

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