0

I am trying to make a programm, that interacts with Windows shell, so I tried these commands:

output = subprocess.run('ipconfig', stdout = subprocess.PIPE)
output.stdout

output = os.popen('ipconfig','r',1).read()

but some of the output (on Russian language) is represented in hex (ASCII):

Wireless LAN adapter \x81\xa5\xe1\xaf\xe0\xae\xa2\xae\xa4\xad\xa0\xef \xe1\xa5\xe2\xec 4:

should be

Wireless LAN adapter Беспроводная сеть 4:

I need all output info to be text.

By the way, when I use os.popen('ipconfig', 'w') in write mode, the output is correct and there is no hex values, just text, but I can't save it to integer.

Some already asked questions, that I found...

Which encoding uses the \x (backslash x) prefix?

Decode Hex String in Python 3

Thank's 4 help!

1 Answers1

1

Try this

output.decode('cp866')

The output from subprocess should be a bytes object (in Python 3), using your example:

b'Wireless LAN adapter \x81\xa5\xe1\xaf\xe0\xae\xa2\xae\xa4\xad\xa0\xef \xe1\xa5\xe2\xec 4:'.decode('cp866')
> 'Wireless LAN adapter Беспроводная сеть 4:'

In general, you need to know which encoding your system is using, cp866 is used for Russian, check https://docs.python.org/3/library/codecs.html#standard-encodings

You can also query your system default encoding with

sys.getdefaultencoding()

And if it matches, you don't even need to specify it when calling bytes.decode.

JoseKilo
  • 2,343
  • 1
  • 16
  • 28