带有波斯语/阿拉伯语字符的 Python 3 print() 函数

新手上路,请多包涵

我简化了我的代码以便更好地理解。这是问题所在:

情况1:

 # -*- coding: utf-8 -*-

text = "چرا کار نمیکنی؟" # also using u"...." results the same
print(text)

输出:

 UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-2: character maps to <undefined>

案例 2:

 text = "چرا کار نمیکنی؟".encode("utf-8")
print(text)

没有输出。

案例 3:

 import sys

text = "چرا کار نمیکنی؟".encode("utf-8")
sys.stdout.buffer.write(text)

输出:

 چرا کار نمیکنی؟


我知道案例 3 以某种方式起作用,但我想使用其他函数,如 print() 、 write(str()) ,….

我还在 这里 阅读了关于 Unicode 的 python 3 文档。

还阅读了 stackoverflow 中的许多问答。

是一篇很长的文章,解释了 python 2.X 的问题和答案

简单的问题是:

如何使用 python print() 函数打印非 ASCII 字符,如波斯语或阿拉伯语?

更新 1:正如许多人所建议的那样,问题与我测试过的终端有关:

案例 4:

 text = "چرا کار نمیکنی؟" .encode("utf-8")# also using u"...." results the same
print(text)

终端 :

 python persian_encoding.py > test.txt

测试.txt:

 b'\xda\x86\xd8\xb1\xd8\xa7 \xda\xa9\xd8\xa7\xd8\xb1 \xd9\x86\xd9\x85\xdb\x8c\xda\xa9\xd9\x86\xdb\x8c\xd8\x9f'

非常重要的更新:

在解决这个问题一段时间后,我终于找到了另一种解决方法来让 cmd.exe 完成这项工作(不需要像 ConEmu 或…这样的第三方软件):

先解释一下:

我们的主要问题与 Python 无关。这是 Windows 中命令提示符字符集的问题(有关完整解释,请查看 Arman 的回答)所以…如果您将 Windows 命令提示符的字符集更改为 UTF-8 而不是默认的 ascii ,那么命令提示符将能够与 UTF-8 字符(如波斯语或阿拉伯语)交互,此解决方案不能保证字符的良好表示(因为它们将像小方块一样打印出来),但如果您想在 python 中进行文件 I/O,这是一个很好的解决方案使用 UTF-8 字符。

脚步:

在从命令行启动 python 之前,键入:

 chcp 65001

现在像往常一样运行你的 python 代码。

 python testcode.py

情况 1 的结果:

 ?????? ??? ??????

它运行没有错误。

截屏:

在此处输入图像描述

有关如何将 65001 设置为默认字符集的更多信息,请查看 内容。

原文由 Soorena 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.5k
2 个回答

您的代码是正确的,因为它可以在我的计算机上使用 Python 2 和 3(我在 OS X 上)运行:

 ~$ python -c 'print "تست"'
تست
~$ python3 -c 'print("تست")'
تست

问题在于您的终端无法输出 unicode 字符。您可以通过将输出重定向到类似 python3 my_file.py > test.txt 的文件并使用编辑器打开该文件来验证它。

如果你在 Windows 上,你可以使用像 Console2ConEmu 这样的终端,它比 Windows 提示更好地呈现 unicode。

由于错误的 Windows 代码页/编码,您也可能会遇到这些终端的错误。有一个小的 python 包可以修复它们(正确设置它们):

1-安装 这个 pip install win-unicode-console

2- 把这个放在你的 python 文件的顶部:

 try:
    # Fix UTF8 output issues on Windows console.
    # Does nothing if package is not installed
    from win_unicode_console import enable
    enable()
except ImportError:
    pass

如果在重定向到文件时出现错误,您可以通过设置 io 编码来修复它:

在 Windows 命令行上:

 SET PYTHONIOENCODING=utf-8

在 Linux/OS X 终端上:

 export PYTHONIOENCODING=utf-8

几点

  • 不需要在 python 3 中使用 u"aaa" 语法。默认情况下,字符串文字是 unicode。
  • 文件的默认编码是 python 3 中的 UTF8,因此不需要编码声明注释(例如 # -*- coding: utf-8 -*- )。

原文由 Arman Ordookhani 发布,翻译遵循 CC BY-SA 3.0 许可协议

输出基本上取决于您运行代码的平台和终端。让我们检查以下针对使用 2.x 或 3.x 运行的不同 Windows 终端的代码片段:

 # -*- coding: utf-8 -*-
import sys

def case1(text):
    print(text)

def case2(text):
    print(text.encode("utf-8"))

def case3(text):
    sys.stdout.buffer.write(text.encode("utf-8"))

if __name__ == "__main__":
    text = "چرا کار نمیکنی؟"

    for case in [case1, case2, case3]:
        try:
            print("Running {0}".format(case.__name__))
            case(text)
        except Exception as e:
            print(e)

        print('-'*80)

结果

Python 2.x

 Sublime Text 3 3122

    Running case1
    'charmap' codec can't encode characters in position 0-2: character maps to <undefined>
    --------------------------------------------------------------------------------
    Running case2
    b'\xda\x86\xd8\xb1\xd8\xa7 \xda\xa9\xd8\xa7\xd8\xb1 \xd9\x86\xd9\x85\xdb\x8c\xda\xa9\xd9\x86\xdb\x8c\xd8\x9f'
    --------------------------------------------------------------------------------
    Running case3
    چرا کار نمیکنی؟--------------------------------------------------------------------------------

ConEmu v151205

     Running case1
    ┌åÏ▒Ϻ ┌®ÏºÏ▒ ┘å┘à█î┌®┘å█îσ
    --------------------------------------------------------------------------------
    Running case2
    'ascii' codec can't decode byte 0xda in position 0: ordinal not in range(128)
    --------------------------------------------------------------------------------
    Running case3
    'file' object has no attribute 'buffer'
    --------------------------------------------------------------------------------

Windows 命令提示符

    Running case1
    ┌åÏ▒Ϻ ┌®ÏºÏ▒ ┘å┘à█î┌®┘å█îσ
    --------------------------------------------------------------------------------

    Running case2
    'ascii' codec can't decode byte 0xda in position 0: ordinal not in range(128)
    --------------------------------------------------------------------------------

    Running case3
    'file' object has no attribute 'buffer'
    --------------------------------------------------------------------------------

Python 3.x

 Sublime Text 3 3122

    Running case1
    'charmap' codec can't encode characters in position 0-2: character maps to <undefined>
    --------------------------------------------------------------------------------
    Running case2
    b'\xda\x86\xd8\xb1\xd8\xa7 \xda\xa9\xd8\xa7\xd8\xb1 \xd9\x86\xd9\x85\xdb\x8c\xda\xa9\xd9\x86\xdb\x8c\xd8\x9f'
    --------------------------------------------------------------------------------
    Running case3
    چرا کار نمیکنی؟--------------------------------------------------------------------------------

ConEmu v151205

     Running case1
    'charmap' codec can't encode characters in position 0-2: character maps to <undefined>
    --------------------------------------------------------------------------------
    Running case2
    b'\xda\x86\xd8\xb1\xd8\xa7 \xda\xa9\xd8\xa7\xd8\xb1 \xd9\x86\xd9\x85\xdb\x8c\xda\xa9\xd9\x86\xdb\x8c\xd8\x9f'
    --------------------------------------------------------------------------------
    Running case3
    ┌åÏ▒Ϻ ┌®ÏºÏ▒ ┘å┘à█î┌®┘å█îσ--------------------------------------------------------------------------------

Windows 命令提示符

    Running case1
    'charmap' codec can't encode characters in position 0-2: character maps to <unde
    fined>
    --------------------------------------------------------------------------------

    Running case2
    b'\xda\x86\xd8\xb1\xd8\xa7 \xda\xa9\xd8\xa7\xd8\xb1 \xd9\x86\xd9\x85\xdb\x8c\xda
    \xa9\xd9\x86\xdb\x8c\xd8\x9f'
    --------------------------------------------------------------------------------

    Running case3
    ┌åÏ▒Ϻ ┌®ÏºÏ▒ ┘å┘à█î┌®┘å█îσ----------------------------------------------------
    ----------------------------

如您所见,仅使用 sublime text3 终端(case3)就可以正常工作。其他终端不支持波斯语。这里的要点是,这取决于您使用的终端和平台。

解决方案(ConEmu 特定)

ConEmu 等现代终端允许您使用 UTF8 编码,如此 处所述,因此,让我们尝试:

 chcp 65001 & cmd

然后针对 2.x 和 3.x 再次运行脚本:

Python2.x

 Running case1
��را کار نمیکنی؟[Errno 0] Error
--------------------------------------------------------------------------------
Running case2
'ascii' codec can't decode byte 0xda in position 0: ordinal not in range(128)
--------------------------------------------------------------------------------
Running case3
'file' object has no attribute 'buffer'
--------------------------------------------------------------------------------

Python3.x

 Running case1
چرا کار نمیکنی؟
--------------------------------------------------------------------------------
Running case2
b'\xda\x86\xd8\xb1\xd8\xa7 \xda\xa9\xd8\xa7\xd8\xb1 \xd9\x86\xd9\x85\xdb\x8c\xda\xa9\xd9\x86\xdb\x8c\xd8\x9f'
--------------------------------------------------------------------------------
Running case3
چرا کار نمیکنی؟--------------------------------------------------------------------------------

如您所见,现在使用 python3 case1(打印)输出成功。所以……寓言的寓意……了解更多关于你的工具以及如何为你的用例正确配置它们的信息;-)

原文由 BPL 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题