python中二进制,Python中的二进制缓冲区

Python中的BytesIO类类似于StringIO,但用于处理二进制数据。当你需要在内存中创建ZIP文件等二进制文件时,BytesIO提供了一个文件对象接口。BytesIO允许你在没有实际磁盘文件的情况下,使用类似文件的操作进行读写操作,这对于处理 ZIP 文件等场景非常有用。

In Python you can use StringIO for a file-like buffer for character data. Memory-mapped file basically does similar thing for binary data, but it requires a file that is used as the basis. Does Python have a file object that is intended for binary data and is memory only, equivalent to Java's ByteArrayOutputStream?

The use-case I have is I want to create a ZIP file in memory, and ZipFile requires a file-like object.

解决方案

You are probably looking for io.BytesIO class. It works exactly like StringIO except that it supports binary data:

from io import BytesIO

bio = BytesIO(b"some initial binary data: \x00\x01")

StringIO will throw TypeError:

from io import StringIO

sio = StringIO(b"some initial binary data: \x00\x01")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值