StringIO在内存中读写字符串
from io import StringIO
sio = StringIO()
sio.write(str)
功能:将字符串写入sio对象中
sio.write("hello")
sio.getvalue()
功能:获取写入的内容
print(sio.getvalue())
sio2.read()
功能:一次性读取所有的sio对象中的内容
sio2 = StringIO("hello jerry!!!")
print(sio2.read())
内存中读取二进制字符串:
方法1:
from io import BytesIO
bio = BytesIO()
bio.write("China".encode("utf-8")) #先编码
print(bio.getvalue().decode("utf-8")) #再解码
>>>China
方法2:
from io import BytesIO
bio2 = BytesIO("China".encode("utf-8"))
print(bio2.read().decode("utf-8"))
>>>China