带有os.popen()的Windows cmd上的python:UnicodeDecodeError:'charmap'编解码器无法解码字节

时间:2019-10-09 16:55:47

标签: python windows cmd character-encoding popen

代码:

import os

ipconfig1 = os.popen("ipconfig /all")
ipconfig2 = ipconfig1.read()

ipconfig输出具有非拉丁符号。错误:

Traceback (most recent call last):
  File "getIt.py", line 4, in <module>
    ipconfig2 = ipconfig1.read()
  File "C:\Program Files (x86)\Python37-32\lib\encodings\cp1250.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 2948: character maps to <undefined>

我尝试过的事情:

pip install win-unicode-console

并且(是的,其中之一很愚蠢,但我拼命尝试了)

ipconfig2 = ipconfig1.decode('utf8').read()
ipconfig2 = ipconfig1.encode('utf8').read()

在bat文件中:chcp 65001set PYTHONIOENCODING=:replaceset PYTHONIOENCODING=utf-8

这些都不起作用。如何正确执行?

0 个答案:

没有答案