简体   繁体   English

在python中从文件读取C结构

[英]Reading C structs from file in python

I have very little experience with C, so I apologize in advance if something I say does not make sense. 我对C的经验很少,所以如果我说的话没有道理,我会提前道歉。

I am working on a project in which my entire base of code is written in python 2.7. 我正在一个项目中,我的整个代码库都是用python 2.7编写的。 I am at a point where I must read in data created by someone else who is using C. Each data file he has given me consists of a header struct, such as... 我现在必须读入使用C的其他人创建的数据。他给我的每个数据文件都包含一个头结构,例如...

struct Header {
    int32_t x1;
    int32_t x2;
    .
    .
    .
};

This is then followed by an array of structs of identical format, each containing data for a single element in a simulation. 然后是相同格式的结构数组,每个结构都包含模拟中单个元素的数据。

Is there a way to read this data in with python? 有没有办法用python读取此数据? It seems like I need to use python's struct module, but I've read through the documentation and either I can't find what I need or don't understand how I should use it. 似乎我需要使用python的struct模块,但是我已经通读了文档,或者找不到所需的内容,或者不了解如何使用它。

Thank you! 谢谢!

看一下struct模块的packunpack函数或ctypes模块-要么可以将您整理出来,但最好取决于几个因素, numpy还包括用于读取“外来”数据的函数,可以提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM