Python io 库

io

官方文档:https://docs.python.org/3/library/io.html

StringIO 和 BytesIO

StringIO 和 BytesIO 是在内存中操作 str 和 bytes 的方法,使得和读写文件具有一致的接口

很多时候,数据读写不一定是文件,也可以在内存中读写。

StringIO 顾名思义就是在内存中读写 str。

要把 str 写入 StringIO,我们需要先创建一个 StringIO,然后,像文件一样写入即可:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
>>> from io import StringIO
>>> f = StringIO()
>>> f.write('hello')
5
>>> f.write(' ')
1
>>> f.write('world!')
6
>>> print(f.getvalue())
hello world!

getvalue()方法用于获得写入后的 str。

要读取 StringIO,可以用一个 str 初始化 StringIO,然后,像读文件一样读取:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
>>> from io import StringIO
>>> f = StringIO('Hello!\nHi!\nGoodbye!')
>>> while True:
...     s = f.readline()
...     if s == '':
...         break
...     print(s.strip())
...
Hello!
Hi!
Goodbye!

BytesIO

StringIO 操作的只能是 str,如果要操作二进制数据,就需要使用 BytesIO。

BytesIO 实现了在内存中读写 bytes,我们创建一个 BytesIO,然后写入一些 bytes:

1
2
3
4
5
6
>>> from io import BytesIO
>>> f = BytesIO()
>>> f.write('中文'.encode('utf-8'))
6
>>> print(f.getvalue())
b'\xe4\xb8\xad\xe6\x96\x87'

请注意,写入的不是 str,而是经过 UTF-8 编码的 bytes。

和 StringIO 类似,可以用一个 bytes 初始化 BytesIO,然后,像读文件一样读取:

1
2
3
4
>>> from io import BytesIO
>>> f = BytesIO(b'\xe4\xb8\xad\xe6\x96\x87')
>>> f.read()
b'\xe4\xb8\xad\xe6\x96\x87'

通用方法

tell()

Return the current stream position.

通常用于获取字节数

truncate(size=None, /)

Resize the stream to the given size in bytes (or the current position if size is not specified). The current stream position isn’t changed. This resizing can extend or reduce the current file size. In case of extension, the contents of the new file area depend on the platform (on most systems, additional bytes are zero-filled). The new file size is returned.

seek(offset, whence=SEEK_SET, /)

Change the stream position to the given byte offset. offset is interpreted relative to the position indicated by whence. The default value for whence is SEEK_SET. Values for whence are:

  • SEEK_SET or 0 – start of the stream (the default); offset should be zero or positive
  • SEEK_CUR or 1 – current stream position; offset may be negative
  • SEEK_END or 2 – end of the stream; offset is usually negative

Return the new absolute position.