如何使用Python读取包含数组的结构';s C类型和读入?

如何使用Python读取包含数组的结构';s C类型和读入?,python,ctypes,Python,Ctypes,我们有一些由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_ul

我们有一些由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中创建结构

根据这一点(第15.15.1.13节数组),它应该类似于:

class strucWithArrays(Structure):
  _fields_ = [
  ("foo", c_ulong),
  ("barFloat", c_float * 4),
  ("bowFloat", c_float * 17)]
查看文档页面中的其他示例。

文档中有一个关于的部分。 基本上这意味着:

class structWithArray(Structure):
    _fields_ = [
      ("foo", c_ulong),
      ("barFloat", c_float * 4),
      ("bowFloat", c_float * 17)
    ]

谢谢不知道我怎么会错过那一次。