将二进制数据视为文件对象? [英] Treat binary data as a file object?

查看:56
本文介绍了将二进制数据视为文件对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在尝试适应自己用途的某些现有代码的重要部分.

Here is the important part of some existing code that I'm trying to adapt for my own uses.

值得注意的部分是self.archive会导致一个庞大的文件,而raw_file是从该巨型文件中提取(痛苦地)的二进制数据.

The notable part is that self.archive leads to a massive file and raw_file is binary data extracted (painfully) from this giant file.

with open(self.archive, "rb") as f:
    f.seek(offset)
    raw_file = start + f.read(dlen - len(start))
    ...
    f.write(raw_file)

现有代码将类似档案的文件的内容提取到磁盘,但是我只需要从该档案中读取这些存储的文件(如果有任何意义).

The existing code extracts the contents of an archive-like file to disk, but I need to only read these stored files from this archive (if that makes any sense).

我需要使用Pygame来使用诸如pygame.image.load()之类的方法从该文件中读取几百mbs的数据,大部分为图像.与其让这段代码将文件的所有内容都写入磁盘,然后以非二进制"的形式再次读取它,不如直接做pygame.image.load(toVirtualFileObject(raw_file))之类的事情.有人知道这样的事吗?

I need to use Pygame to read a few hundred mbs of data from this file, mostly as images, using methods such as pygame.image.load(). Rather than having this code write all of the file's contents to disk and then re-reading it again in "not-binary", I'd like to directly do something like pygame.image.load(toVirtualFileObject(raw_file)). Does anyone know of something like this?

推荐答案

这是 StringIO (在Python 2中)和 io.BytesIO (在Python 3中)用于.

This is what StringIO (in Python 2) and io.BytesIO in (in Python 3) are for.

这篇关于将二进制数据视为文件对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆