from io import StringIO
a = "hello world"
sio = StringIO(a)
print(sio)
print(sio.tell()) # 获取当前文件读取指针的位置
print(sio.read()) # 从当前位置开始读取字符串
print(sio.getvalue()) # 返回可变字符串的全部内容
sio.seek(6)
print(sio.tell())
print(sio.read())
sio.write("python") # 从seek(6)指定的位置开始写入字符串
print(sio.tell())
print(sio.read())
print(sio.getvalue())
结果:
<_io.StringIO object at 0x0000000002150F78>
0
hello world
hello world
6
world
17
hello worldpython
网友评论