我很难将 subprocess.run
函数与包含重音字符(例如“é”)的命令一起使用。
考虑这个简单的例子:
# -*- coding: utf-8 -*-
import subprocess
cmd = "echo é"
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)
print("Output of subprocess.run : {}".format(result.stdout.hex()))
print("é char encoded manually : {}".format("é".encode("utf-8").hex()))
它给出以下输出:
Output of subprocess.run : 820d0a
é char encoded manually : c3a9
我不明白subprocess.run
返回的值,它不应该也是c3a9
吗?我知道 0d0a
是 CR+LF,但为什么是 82
?
因此,当我尝试运行这一行时:
output = result.stdout.decode("utf-8")
我收到一个 UnicodeDecodeError 异常,消息如下:'utf-8' codec can't decode byte 0x82 in position 0: invalid start byte
我试过像这样明确指定编码格式:
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, encoding="utf-8")
但这会在调用 subprocess.run
时引发相同的异常('utf-8' 编解码器无法解码位置 0 中的字节 0x82:无效的起始字节
) .
我在 Windows 10 上使用 Python3.8.5 运行它。
我希望有人能帮我解决这个问题,有什么提示吗?
最佳答案
作为修复尝试 cp437 解码:
print("Output of subprocess.run : {}".format(result.stdout.decode('cp437')))
# or
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, text=True,
encoding="cp437")
print(f"Output of subprocess.run : {result.stdout}")
从其他 stackoverlow 答案来看,Windows 终端代码问题似乎是老问题,现在可能应该修复,但它似乎仍然存在。
https://stackoverflow.com/a/37260867/11815313
反正我对Windows 10终端编码没有深入了解,但是cp437在我的Win10系统上是行得通的。
然而 Python 3.9.13 documentation 3. Using Python on Windows 3.7. UTF-8 mode说明临时或永久(注意文档中提到的警告)更改编码的选项。
关于python - UTF-8 编码异常与 subprocess.run,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73545218/