【发布时间】:2017-05-02 21:24:07
【问题描述】:
我尝试逐行读取子进程:
proc = subprocess.Popen(self.monitor_logcat_cmd, shell=True, stdout=subprocess.PIPE,
bufsize=1, universal_newlines=True)
while proc.poll() is None:
line = proc.stdout.readline()
print("Process line: " + str(line))
它有效,但在某些时候我得到错误:
Exception in thread Thread-14:
Traceback (most recent call last):
File "/Users/F1sherKK/anaconda3/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/Users/F1sherKK/Dev/Python/AutomationTestSupervisor/session/SessionThreads.py", line 46, in run
line = proc.stdout.readline()
File "/Users/F1sherKK/anaconda3/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 89: invalid start byte
有没有办法为子进程的标准输出添加/指定编码?我想添加错误“忽略”。
还有其他方法可以解决这个问题吗?
【问题讨论】:
-
那么进程会产生什么字节,关闭
universal_newlines?你知道这个过程会产生什么编码吗? -
monitor_logcat_cmd究竟包含什么?在 shell 中运行什么命令?你是在设置LANG还是LC_CTYPE环境变量? -
monitor_logcat_cmd 是
adb -s 5554 logcat它正在从Android设备实时读取日志。我猜它可以由各种编码组成。例如,日志中可以有表情符号。我没有设置任何环境变量。
标签: python encoding subprocess stdout popen