问题描述
我正在尝试通过 Mac 上的 stdio 从 Python 脚本中读取数据(共享是因为我阅读了操作系统有所不同)。我尝试通过
读取传入流sys.stdin.read(NUM_BYTES)
和
input()
对于两者我都得到错误
File "/Library/Developer/CommandLinetools/Library/Frameworks/python3.framework/Versions/3.7/lib/python3.7/codecs.py",line 322,in decode
(result,consumed) = self._buffer_decode(data,self.errors,final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcb in position 3: invalid continuation byte
我检查了发送数据的脚本中的编码类型,它显示“无”,因为它没有设置。
那么如何读取未经任何编码发送的数据?而无需更改发送方脚本中的编码。
解决方法
如果您将其作为字符串读取,则您使用的是一种编码(无论是否在写入时指定)。
sys.stdin.buffer.read()
将 STDIN 作为二进制读取。 sys.stdin.buffer
是 io.BufferedReader
。