python学习之io模块

class io.BytesIO([initial_bytes])

他是一个_io.BytesIO对象。

用这个类的实例可以操作内存缓冲区中的字节流。

>>> s = 'hello'
>>> b = s.encode()
>>> b
b'hello'
>>> import io
>>> a = io.BytesIO(b)
>>> a
<_io.BytesIO object at 0x00000064DF5DA2B0>
>>> c = a.getbuffer()
>>> c
<memory at 0x00000064DF5F0648>
>>> n = '12'
>>> b1 = n.encode()
>>> c[1:3] = b1
>>> a.getvalue()
b'h12lo'

使用该类的实例可以创建缓冲区并在缓冲区中操作字节流。

常见方法

getvalue()

返回包含缓冲区全部内容的字节。

read1(n)

与read()相同,但是必须要传入一个参数。

read([n])

返回指定个数的字符,类型为字节流。

原文地址:https://www.cnblogs.com/leomei91/p/7615173.html