我正在使用子进程运行一个进程:p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
我要做的是在循环中逐个读取输出字符:
^{pr2}$
在python3中subprocess.Popen().stdout.read()返回bytes()而不是str()。我想用它作为str,所以我必须:char = char.decode("utf-8")
它可以很好地处理ascii字符。在
但是对于非ascii字符(例如希腊字母),我得到一个UnicodeDecodeError。这就是为什么希腊字符由多个字节组成。问题是:>>> b'\xce\xb5'.decode('utf-8')
'ε'
>>> b'\xce'.decode('utf-8') # b'\xce' is what subprocess...read(1) returns - one byte
Traceback (most recent call last):
File "", line 1, in
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 0: unexpected end of data
>>>
我该怎么处理?
subprocess.Popen().stdout.read()(作为一个字符串)的输出可以类似于“lorem ipsumεφΔφδδσloremipsum”。在
我想一次读取一个字符,但是这个字符可以由多个字节组成。在