我简化了我的代码以便更好地理解.这是问题所在:
情况1:
# -*- coding: utf-8 -*-
text = "??? ??? ???????" # also using u"...." results the same
print(text)
Run Code Online (Sandbox Code Playgroud)
输出:
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-2: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)
案例2:
text = "??? ??? ???????".encode("utf-8")
print(text)
Run Code Online (Sandbox Code Playgroud)
没有输出.
案例 3:
import sys
text = "??? ??? ???????".encode("utf-8")
sys.stdout.buffer.write(text)
Run Code Online (Sandbox Code Playgroud)
输出:
??? ??? ???????
Run Code Online (Sandbox Code Playgroud)
我知道案例3以某种方式工作,但我想使用其他函数,如print(),write(str()),....
我也读蟒蛇3关于为Unicode文件在这里.
并且还在stackoverflow中阅读了几十个问答.
而这里是一个很长的文章解释蟒蛇2.X的问题和答案
简单的问题是:
如何使用python print()函数打印波斯语或阿拉伯语等非ASCII字符?
更新1:正如许多人建议的那样,问题与我测试案例的终端有关:
案例4:
text = "??? ??? ???????" .encode("utf-8")# also using u"...." results the …Run Code Online (Sandbox Code Playgroud)