如何使用Python的ctypes和readinto读取包含数组的结构?
发布时间:2021-01-11 23:35:10 所属栏目:Python 来源:互联网
导读:我们有一些由C程序创建的二进制文件. 通过调用fwrite将以下C结构写入文件来创建一种类型的文件: typedef struct { unsigned long int foo; unsigned short int bar; unsigned short int bow;} easyStruc; 在Python中,我读取此文件的结构如下: class easyStr
我们有一些由C程序创建的二进制文件. 通过调用fwrite将以下C结构写入文件来创建一种类型的文件: typedef struct { unsigned long int foo; unsigned short int bar; unsigned short int bow; } easyStruc; 在Python中,我读取此文件的结构如下: class easyStruc(Structure): _fields_ = [ ("foo",c_ulong),("bar",c_ushort),("bow",c_ushort) ] f = open (filestring,'rb') record = censusRecord() while (f.readinto(record) != 0): ##do stuff f.close() 这很好.我们的其他类型的文件使用以下结构创建: typedef struct { // bin file (one file per year) unsigned long int foo; float barFloat[4]; float bowFloat[17]; } strucWithArrays; 我不确定如何在Python中创建结构. 解决方法根据这个 documentation page(部分:15.15.1.13.数组),它应该是这样的:class strucWithArrays(Structure): _fields_ = [ ("foo",("barFloat",c_float * 4),("bowFloat",c_float * 17)] 查看该文档页面以获取其他示例. (编辑:台州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- python:re.sub的replace函数不接受额外的参数 – 如何避免
- django – Travis:“创建测试数据库时出错:创建数据库的权
- python – Google App Engine中模型的默认值
- python – sqlalchemy在yield_per期间发生游标错误
- python – igraph:为什么add_edge函数如此缓慢地对add_edg
- Python – 立即引导大量敌人
- python – Matplotlib:从头开始制作彩色标记图例
- 如何生成字符之间带空格的字符串的所有可能组合?Python
- python – 如何将列表或字符串解析为固定长度的块
- Python运算符重载用法实例分析