在python中将字节转换为BufferedReader
我有一个字节数组,想转换成一个缓冲读取器。一种方法是将字节写入文件并再次读取在python中将字节转换为BufferedReader,python,arrays,file-io,io,buffer,Python,Arrays,File Io,Io,Buffer,我有一个字节数组,想转换成一个缓冲读取器。一种方法是将字节写入文件并再次读取 sample_bytes = bytes('this is a sample bytearray','utf-8') with open(path,'wb') as f: f.write(sample_bytes) with open(path,'rb') as f: extracted_bytes = f.read() print(type(f)) 输出: <class '_io.Buffer
sample_bytes = bytes('this is a sample bytearray','utf-8')
with open(path,'wb') as f:
f.write(sample_bytes)
with open(path,'rb') as f:
extracted_bytes = f.read()
print(type(f))
输出:
<class '_io.BufferedReader'>
但我得到一个属性错误
AttributeError: 'bytes' object has no attribute 'readable'
如何在不将字节保存到本地磁盘的情况下将字节写入和读取到类似文件的对象中?如果您要查找的只是内存中类似文件的对象,我将查看
from io import BytesIO
file_like = BytesIO(b'this is a sample bytearray')
print(file_like.read())
导入io;file\u like=io.BytesIO(示例字节)
from io import BytesIO
file_like = BytesIO(b'this is a sample bytearray')
print(file_like.read())