Python subprocess decode reported an error?

python rookie, while watching the python subprocess tutorial of Liao Xuefeng, I encountered the following error report when executing nslookup command

operating system: win10 python version: 3.7
import subprocess
print("$ nslookup")
p = subprocess.Popen(["nslookup"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = p.communicate(b"set q=mx\npython.org\nexit\n")
print(output.decode("utf-8"))
print("Exit code:", p.returncode)

error message:

$ nslookup
Traceback (most recent call last):
  File "E:/py/learning-python/Process_Thread/multi_process/sub_process.py", line 14, in <module>
    print(output.decode("utf-8"))
UnicodeDecodeError: "utf-8" codec can"t decode byte 0xc8 in position 2: invalid continuation byte

Process finished with exit code 1

what is the cause of this error? How to solve it? thank you.


< H2 > old friend, which has nothing to do with subprocess , is actually a coding problem of bytes class conversion to str < / H2 >.

try it:

>>> print(output.decode('gbk'))

:  dns.xxx.xxx.cn
Address:  xxx.xxx.xxx.xxx

> > :  dns.xxx.xxx.cn
Address:  xxx.xxx.xxx.xxx

python.org    MX preference = xx, mail exchanger = mail.python.org

python.org    nameserver = nsx.pxx.dynect.net
python.org    nameserver = nsx.pxx.dynect.net
python.org    nameserver = nsx.pxx.dynect.net
python.org    nameserver = nsx.pxx.dynect.net
> 

this string contains with Chinese .

and in this sentence output, err = p.communicate (b'set q=mx\ npython.org\ nexit\ n') , output is encoded as GBK

so utf-8 cannot be used to decode

.

this is actually the problem of gbk coding and utf-8 coding causing conflict

.

all support Chinese, but encodes in different order. Let me give you an example:

>>> ''.encode()    
b'\xe6\x88\x91'
>>> ''.encode('gbk')
b'\xce\xd2'

you can refer to this boss's article: https://www.cnblogs.com/gavin....

good luck!


decode uses strict mode by default. If the second parameter is changed to ignore , it can be decoded normally. The specific reason is unknown.

print(output.decode('utf-8','ignore'))
Menu