程序上传到ubuntu服务器本地没有错误,但上传到服务器上出现了UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
的错误。
在python3的环境下查看默认输出的字符编码:
import sys
sys.stdout.encoding
发现默认的字符编码是ANSI_X3.4-1968。不支持中文。应该是utf-8.
在django项目的setting中配置字符编码:
import sys,io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
重启服务就可以了。