Discuss / Python / StringIO, BytesIO

StringIO, BytesIO

Topic source

Zack_Chang

#1 Created at ... [Delete] [Delete and Lock User]

总结:

从内存中读写string或者bytes与直接读写文件类似,有相同的接口

from io import StringIO
f = StringIO()
f.write('Hello world!')  #此时stream position移动到12
print(f.getvalue())
s = f.read()   #从stream position=12的位置读取f内容,s = ''
 
f.seek(0) #将stream position拨回到0
s = f.read()  #从头开始读起f内容,s = 'Hello world!'

  • 1

Reply