在 mingw64 中,python 脚本无法 print() UTF-8 [Windows 10]

问题描述 投票:0回答:1
  1. https://www.msys2.org/

    安装 MSYS2
  2. 打开 mingw64.exe 终端,并进行常规设置:

    pacman -Syu
    pacman -Su
    pacman -S mingw-w64-x86_64-python3
    
  3. 运行Python脚本

    python3 test.py
    

剧本是

#!/usr/bin/env python3
print("가요대제전")
  1. 我们得到了这个回溯
$ python3 testme.py
Traceback (most recent call last):
  File "C:/msys64/home/user/testme.py", line 2, in <module>
    print("\uac00\uc694\ub300\uc81c\uc804")
  File "C:/msys64/mingw64/lib/python3.11/encodings/cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-4: character maps to <undefined>

使用

.encode()
可以解决回溯问题,但会产生乱码输出:

print("가요대제전".encode("utf-8"))

b'\xea\xb0\x80\xec\x9a\x94\xeb\x8c\x80\xec\xa0\x9c\xec\xa0\x84'
python-3.x utf-8 mingw msys2
1个回答
0
投票

解决办法是

python3 -X utf8 test.py
© www.soinside.com 2019 - 2024. All rights reserved.