Discuss / Python / ‘utf-8’出现错误,改为‘gbk’后可行

‘utf-8’出现错误,改为‘gbk’后可行

Topic source

G-Master-G

#1 Created at ... [Delete] [Delete and Lock User]

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)

运行后出现 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 2: invalid continuation byte

改为gbk后可行,运行结果如下 $ nslookup 默认服务器: 69.226.101.202.other.jj.jx.dynamic.163data.com.cn Address: 202.101.226.69

服务器: 69.226.101.202.other.jj.jx.dynamic.163data.com.cn Address: 202.101.226.69

python.org MX preference = 50, mail exchanger = mail.python.org > Exit code: 0

zwq54argfun

#2 Created at ... [Delete] [Delete and Lock User]

我也碰到了同样的问题,不知是否和操作系统有关,用的win7 32bit

木禾米粥

#3 Created at ... [Delete] [Delete and Lock User]

我也是啊,我用的是Windows7的,64位的

xigaohu

#4 Created at ... [Delete] [Delete and Lock User]

应该是进行请求的返回字端中有中文,我的也出现这种情况了

渡俊

#5 Created at ... [Delete] [Delete and Lock User]

谢谢。

天罡拳

#6 Created at ... [Delete] [Delete and Lock User]

这个太赞了,想了一会才想通


  • 1

Reply