python中运行错误: 'utf-8' codec can't decode byte 0xc8'

xiaoxiao2021-02-28  83

错误提示:

# coding=utf-8 import subprocess print('$ nslookup') p = subprocess.Popen(['nslookup'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Communicate()返回一个元组:(stdoutdata, stderrdata) output, err = p.communicate(b'set q=mx\npython.org\nexit\n') print(output.decode('utf-8')) print('Exit code:', p.returncode)

经查询:我所用的系统是windows,通常简体中文windows采用GB2312,极个别文本字符采用utf-8字符集,可以通过chcp的命令端口查询得到 ”活动代码页为:936” 而936对应的就是: 936 中国 - 简体中文(GB2312) 而GBK是GB2312的升级版

所以,把上述的解码改为GBK即可: print(output.decode(‘GBK’))

运行结果:

转载请注明原文地址: https://www.6miu.com/read-41874.html

最新回复(0)