<分区>
我有代码可以打开二进制文件并从中读取文件。
with open (file, mode="rb") as myfile:
message_string=myfile.read()
myfile.close
我现在需要做同样的事情从标准输入读取。但我不知道如何读取二进制文件。
错误仅显示字节字符串。
有什么建议吗?
<分区>
我有代码可以打开二进制文件并从中读取文件。
with open (file, mode="rb") as myfile:
message_string=myfile.read()
myfile.close
我现在需要做同样的事情从标准输入读取。但我不知道如何读取二进制文件。
错误仅显示字节字符串。
有什么建议吗?
最佳答案
在Python 3中,如果你想从stdin
中读取二进制数据,你需要使用它的buffer
属性:
import sys
data = sys.stdin.buffer.read()
在 Python 2 上,sys.stdin.read()
已经返回一个字节串;无需使用 buffer
。
关于python - 将标准输入读取为二进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32282448/
相关文章:
python - Python 中的 REGEX 仅匹配指数
Windows PowerShell : How to do standard-in redirection & passing parameters to commands properly?
python - Python中如何防止一段代码被KeyboardInterrupt打断?
python - pynfs : error: gssapi/gssapi. h: 没有那个文件或目录
python - 如何从Python中的字符串生成13位数字