python3 中文乱码 UnicodeEncodeError: ‘latin-1‘ codec can‘t encode characters

报错

#!/usr/bin/python3
# -*- coding: utf-8 -*-

str_1 = '这是中文' # str
print(str_1)

报错:
python3 中文乱码 UnicodeEncodeError: ‘latin-1‘ codec can‘t encode characters
UnicodeEncodeError: ‘latin-1’ codec can’t encode characters in position 0-3: ordinal not in range(256)

解决方法

str_2 = str_1.encode(“utf-8”).decode(“latin1”)

str_1 = '这是中文' # str
str_2 = str_1.encode("utf-8").decode("latin1")
print(str_2)

输出:
python3 中文乱码 UnicodeEncodeError: ‘latin-1‘ codec can‘t encode characters

上一篇:编码错误UnicodeDecodeError: 'gbk' codec


下一篇:python : 'ascii' codec can't de