Python - 如何逐字节编辑十六进制文件

发布于 2024-08-03 14:04:11 字数 123 浏览 7 评论 0原文

我希望能够打开一个图像文件并逐字节添加十六进制值。我不知道如何做到这一点,并且谷歌搜索“python字节编辑”和“python字节数组”没有得出任何结果,令人惊讶。有人可以指出我需要使用的库、我可以谷歌搜索的具体方法或教程/指南吗?

I want to be able to open up an image file and extra the hexadecimal values byte-by-byte. I have no idea how to do this and googling "python byte editing" and "python byte array" didn't come up with anything, surprisingly. Can someone point me towards the library i need to use, specific methods i can google, or tutorials/guides?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

上课铃就是安魂曲 2024-08-10 14:04:11

Python 标准库有 mmap 模块,可以用来完成此任务。请查看文档以获取更多信息。

Python standard library has mmap module, which can be used to do exactly this. Take a look on the documentation for further information.

人事已非 2024-08-10 14:04:11

根据您想要执行的操作,以二进制模式打开文件 并使用正常的 file 函数读取数据:

# load it
with open("somefile", 'rb') as f:
    data = f.read()

# do something with data
data.reverse()

# save it
with open("somefile.new", 'wb') as f:
    f.write(data)

Python 并不真正关心 data 字符串是否包含“二进制”或“文本”数据。如果您只想对合理大小的文件进行简单修改,这可能就足够了。

Depending on what you want to do it might be enough to open the file in binary mode and read the data with the normal file functions:

# load it
with open("somefile", 'rb') as f:
    data = f.read()

# do something with data
data.reverse()

# save it
with open("somefile.new", 'wb') as f:
    f.write(data)

Python doesn't really care if the data string contains "binary" or "text" data. If you just want to do simple modifications to a file of reasonable size this is probably good enough.

终陌 2024-08-10 14:04:11

Hachoir 框架是一组用于解析和编辑二进制文件的 Python 库和工具:

http://pypi .python.org/pypi/hachoir-core

它具有常见文件类型的知识,因此这可能正是您所需要的。

The Hachoir framework is a set of Python library and tools to parse and edit binary files:

http://pypi.python.org/pypi/hachoir-core

It has knowledge of common file types, so this could just be what you need.

请持续率性 2024-08-10 14:04:11

查看 stuct 模块。

此模块执行 Python 值和表示为 Python 字符串的 C 结构之间的转换。它使用格式字符串(如下所述)作为 C 结构布局以及与 Python 值之间的预期转换的紧凑描述。这可用于处理存储在文件中或来自网络连接等来源的二进制数据。

Check out the stuct module.

This module performs conversions between Python values and C structs represented as Python strings. It uses format strings (explained below) as compact descriptions of the lay-out of the C structs and the intended conversion to/from Python values. This can be used in handling binary data stored in files or from network connections, among other sources.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文