Python subprocess check_output decoding specials characters

2024/9/21 12:28:25

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.

Answer

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://en.xdnf.cn/q/72061.html

Related Q&A

Python - SystemError: NULL result without error in PyObject call

The story: Im trying to interface from C to Python in order to use the faster computational speed of C for an existing Python code. I already had some success, also with passing NumPy arrays - but now …

Fix jumping of multiple progress bars (tqdm) in python multiprocessing

I want to parallelize a task (progresser()) for a range of input parameters (L). The progress of each task should be monitored by an individual progress bar in the terminal. Im using the tqdm package f…

How to access data stored in QModelIndex

The code below create a single QListView with the data and proxy models "attached". Clicking one of the radio buttons calls for buttonClicked() function. This function calls models .data(inde…

Pythons read and write add \x00 to the file

I have come across a weird problem when working with files in python. Lets say I have a text file and a simple piece of code that reads the contents of the file and then rewrites it with unaltered cont…

NetworkX remove attributes from a specific node

I am having a problem with networkX library in python. I build a graph that initialises some nodes, edges with attributes. I also developed a method that will dynamic add a specific attribute with a sp…

How to use Python left outer join using FOR/LIST/DICTIONARY comprehensions (not SQL)?

I have two tuples, details below:t1 = [ [aa], [ff], [er] ]t2 = [ [aa, 11,], [er, 99,] ]and I would like to get results like these below using python method similar to SQLs LEFT OUTER JOIN:res = [ [aa, …

GaussianMixture initialization using component parameters - sklearn

I want to use sklearn.mixture.GaussianMixture to store a gaussian mixture model so that I can later use it to generate samples or a value at a sample point using score_samples method. Here is an exampl…

How to use geopy vicenty distance over dataframe columns?

I have a dataframe with location column which contains lat,long location as followsdeviceid location 1102ADb75 [12.9404578177, 77.5548244743]How to get the di…

Opening a postgres connection in psycopg2 causes python to crash

Im getting the following error message when I try to open up a connection to a postgres database. Perhaps its related to OpenSSL, but I cant understand the error message. Can anyone help?>>>…

Calling generated `__init__` in custom `__init__` override on dataclass

Currently I have something like this: @dataclass(frozen=True) class MyClass:a: strb: strc: strd: Dict[str, str]...which is all well and good except dicts are mutable, so I cant use my class to key anot…