我有这段代码:
def __executeCommand(self, command: str, input: str = None) -> str:
p = sub.Popen(command, stdout=sub.PIPE, stderr=sub.PIPE, stdin=sub.PIPE, universal_newlines=True)
p.stdin.write(input)
output, error = p.communicate()
if (len(errors) > 0):
raise EnvironmentError("Could not generate the key: " + error)
elif (p.returncode != 0):
raise EnvironmentError("Could not generate the key. Return Value: " + p.returncode)
return output
我在行output, error = p.communicate()
中得到了一个UnicodeDecodeError:
Traceback (most recent call last):
File "C:\Python34\lib\threading.py", line 921, in _bootstrap_inner
self.run()
File "C:\Python34\lib\threading.py", line 869, in run
self._target(*self._args, **self._kwargs)
File "C:\Python34\lib\subprocess.py", line 1170, in _readerthread
buffer.append(fh.read())
File "C:\Python34\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 27: character maps to <undefined>
我该如何解决这个问题?
答案 0 :(得分:3)
univeral_newlines=True
启用文字模式。使用locale.getpreferredencoding(False)
字符编码将子进程输出(字节)解码为@cdosborn mentioned。
如果它不起作用,请提供encoding
使用的实际command
。和/或将错误处理程序(例如'ignore'
,'surrogateescape'
, etc)指定为errors
参数:
from subprocess import Popen, PIPE
def __executeCommand(self, command: str, input: str = None,
encoding=None, errors='strict') -> str:
text_mode = (encoding is None)
with Popen(command, stdout=PIPE, stderr=PIPE, stdin=PIPE,
universal_newlines=text_mode) as p:
if input is not None and not text_mode:
input = input.encode(encoding, errors) # convert to bytes
output, err = p.communicate(input)
if err or p.returncode != 0:
raise EnvironmentError("Could not generate the key. "
"Error: {}, Return Value: {}".format(
ascii(err), p.returncode))
return output if text_mode else output.decode(encoding, errors)
答案 1 :(得分:2)
universal_newlines=true
设置会导致出现错误来源的其他编码。
def __executeCommand(self, command: str, input: str = None) -> str:
p = sub.Popen(command, stdout=sub.PIPE, stderr=sub.PIPE, stdin=sub.PIPE)
output, error = p.communicate(input)
if (len(errors) > 0):
raise EnvironmentError("Could not generate the key: " + error)
elif (p.returncode != 0):
raise EnvironmentError("Could not generate the key. Return Value: " + p.returncode)
return output
universal_newlines=true
会根据输出结果进行编码:
python -c 'import locale; print locale.getpreferredencoding()'
当Python期望您的输入与上面的编码匹配时,Python会抛出错误,而是以不同的编码清楚地处理一个字节。
有关python 3.4 universal_newlines
here。
答案 2 :(得分:0)
如果您使用的是Python 3.6或更高版本,则可以通过更改以下行来修复错误:
p = sub.Popen(command, stdout=sub.PIPE, stderr=sub.PIPE, stdin=sub.PIPE, universal_newlines=True)
对此:
p = sub.Popen(command, stdout=sub.PIPE, stderr=sub.PIPE, stdin=sub.PIPE, encoding="utf-8", universal_newlines=True)
我在上面使用了UTF-8,但是您可以将其替换为所需的任何编码。