python笔记之BytesIO

1. 什么是BytesIO

BytesIO与StringIO类似,不同的是StringIO只能存放string,BytesIO是用来存放bytes的,它提供了在内存中读写字节的能力。

即在内存中读写字符串使用StringIO,读写bytes使用BytesIO。

2. 如何使用

from io import BytesIO

if __name__ == '__main__':
    buff = BytesIO()
    buff.write(b'hello, python')

    s = buff.read()
    print(s)

    s = buff.getvalue()
    print(s)

    buff.seek(0)
    s = buff.read()
    print(s)

参考资料:

1. https://docs.python.org/2/library/io.html

2. https://www.zhihu.com/question/49102468

.

原文地址:https://www.cnblogs.com/cc11001100/p/7789230.html