什么是Python的结构模块合适的缓冲 [英] What is a suitable buffer for Python's struct module
问题描述
在Python中,我通过读入一个字符串,然后使用 struct.unpack(...)访问的二进制文件
。现在我想写使用 struct.pack_into(...)
该字符串,但我得到了错误的不能使用字符串作为修改缓冲区。什么是用于与结构
模块使用合适的缓冲?
In Python I'm accessing a binary file by reading it into a string and then using struct.unpack(...)
. Now I want to write to that string using struct.pack_into(...)
, but I get the error "Cannot use string as modifiable buffer". What would be a suitable buffer for use with the struct
module?
推荐答案
如果你不是试图把它打包到一个特定的对象,只是使用的 struct.pack
返回一个字符串。
If you aren't trying to pack it into a specific object, just use struct.pack
to return a string.
另外,<一个href=\"http://docs.python.org/library/ctypes.html#ctypes.create%5Fstring%5Fbuffer\"><$c$c>ctypes.create_string_buffer$c$c>是为了获得一个可变缓冲器的一种方式。
Otherwise, ctypes.create_string_buffer
is one way to obtain a mutable buffer.
这篇关于什么是Python的结构模块合适的缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!