如何在Python中打印像文件这样的符号

Jes*_*din 6 python symbols

我正在尝试将符号写入?python中的文本文件.我认为它与编码有关(utf-8).这是代码:

# -*- coding: utf-8 -*-
outFile = open('./myFile.txt', 'wb')
outFile.write("?")
outFile.close()
Run Code Online (Sandbox Code Playgroud)

而不是"?"我得到的黑色"â—".我怎样才能解决这个问题?

Ele*_*ito 1

如果您使用的是 Python 2,请使用codecs.open代替openunicode代替str

\n\n
# -*- coding: utf-8 -*-\nimport codecs\noutFile = codecs.open(\'./myFile.txt\', \'wb\', \'utf-8\')\noutFile.write(u"\xe2\x97\x8f")\noutFile.close()\n
Run Code Online (Sandbox Code Playgroud)\n\n

在 Python 3 中,将encoding关键字参数传递给open

\n\n
# -*- coding: utf-8 -*-\noutFile = open(\'./myFile.txt\', \'w\', encoding=\'utf-8\')\noutFile.write("\xe2\x97\x8f")\noutFile.close()\n
Run Code Online (Sandbox Code Playgroud)\n