简体   繁体   English

Python子进程check_output解码特殊字符

[英]Python subprocess check_output decoding specials characters

I'm having some issues with python encoding. 我有一些python编码的问题。 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"

(i'm french) (我是法国人)

When I try decoding it with a .decode() at the end, it gives me this error: 当我尝试在最后用.decode()解码时,它给了我这个错误:

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

I tried using .decode("utf-8") , I played around with encoding and decoding for hours, and I can't find the answer. 我尝试使用.decode("utf-8") ,我玩了几个小时的编码和解码,我找不到答案。 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? 我该怎么做才能摆脱那些解码错误并将我的特殊字符打印出来?

Thanks. 谢谢。

You're invoking the command through CMD, which has a Unicode mode and an ANSI mode. 您正在通过CMD调用该命令,CMD具有Unicode模式和ANSI模式。 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. “正确”方法是调用Unicode模式,但您可以将encoding="437"encoding="850"到子进程调用中以使其工作。 This depends on you knowing what the current codepage is. 这取决于您知道当前代码页是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM