Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subprocess check_output decoding specials characters

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.

like image 256
Nim Avatar asked Sep 28 '17 19:09

Nim


Video Answer


1 Answers

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.

like image 100
Josh Lee Avatar answered Sep 28 '22 10:09

Josh Lee