将二进制数据视为文件对象?

编程入门 行业动态 更新时间:2024-10-28 06:24:37
本文介绍了将二进制数据视为文件对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

更多推荐

将二进制数据视为文件对象?

本文发布于:2023-11-03 07:58:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554626.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   文件   二进制数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!