Python - 如何逐字节编辑十六进制文件
我希望能够打开一个图像文件并逐字节添加十六进制值。我不知道如何做到这一点,并且谷歌搜索“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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.
根据您想要执行的操作,以二进制模式打开文件 并使用正常的 file 函数读取数据:
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:
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.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.
查看 stuct 模块。
Check out the stuct module.