Python子流程check_output解码特殊字符 [英] Python subprocess check_output decoding specials characters

查看:294
本文介绍了Python子流程check_output解码特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python编码方面遇到一些问题。当我尝试执行此操作时:

I'm having some issues with python encoding. When I try to execute this:

subprocess.check_output("ipconfig", shell=True)

它为我提供了带有特殊字符的输出,例如:

it gives me an output with special characters in it, like:

"Statut du m\x82dia"
"M\x82dia d\x82connect\x82"

(我是法国人)

当我尝试使用 .decode解码时( )最后,它给了我这个错误:

When I try decoding it with a .decode() at the end, it gives me this error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x82 in position 78: invalid start byte

我尝试使用 .decode( utf-8),我花了几个小时进行编码和解码,但找不到答案。我在互联网上看到的所有内容均无效。也许我只是傻瓜,但嘿。
我该怎么做才能摆脱那些解码错误并打印我的特殊字符?

I tried using .decode("utf-8"), I played around with encoding and decoding for hours, and I can't find the answer. Everything I looked on the internet didn't work. Maybe I'm just dumb, but hey. What can I do to get rid of those decoding errors and get my special characters to be printed?

谢谢。

推荐答案

您正在通过CMD调用命令,CMD具有Unicode模式和ANSI模式。 正确的方法是调用Unicode模式,但是您可以添加 encoding = 437 encoding = 850 子过程调用以使其工作。这取决于您是否知道当前的代码页。

You're invoking the command through CMD, which has a Unicode mode and an ANSI mode. The "correct" way is to invoke the Unicode mode, but you can add encoding="437" or encoding="850" to the subprocess call to make it work. This depends on you knowing what the current codepage is.

这篇关于Python子流程check_output解码特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆