什么是Python的结构模块合适的缓冲 [英] What is a suitable buffer for Python's struct module

查看:355
本文介绍了什么是Python的结构模块合适的缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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是为了获得一个可变缓冲器的一种方式。

Otherwise, ctypes.create_string_buffer is one way to obtain a mutable buffer.

这篇关于什么是Python的结构模块合适的缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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