Python subprocess check_output decoding specials characters

霸气de小男生 提交于 2019-12-06 03:29:40

问题


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"

(i'm french)

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

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?

Thanks.


回答1:


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.



来源:https://stackoverflow.com/questions/46476677/python-subprocess-check-output-decoding-specials-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!