从在c结构阅读蟒蛇结构 [英] reading struct in python from created struct in c
问题描述
我在使用Python和很生疏用C很新,所以我事先怎么哑和/或丢失我的声音。
I am very new at using Python and very rusty with C, so I apologize in advance for how dumb and/or lost I sound.
我在C函数创建一个包含数据的.dat文件。我使用Python读取文件打开文件。其中一个我需要阅读的东西是在C函数创建,并以二进制打印的结构。在我的Python code,我在文件的相应行中的结构来阅读。我试图通过这两个项目,并作为一个整体没有成功拆箱stuct项目。大多数在结构中的项目被宣布在C code真实。我对这个code工作与其他人及主要来源$ C $ c是他的,并已声明的变量是真实的。我需要把它放到一个循环,因为我想读所有的目录,在.DAT结尾的文件。要启动回路我有:
I have function in C that creates a .dat file containing data. I am opening the file using Python to read the file. One of the things I need to read are a struct that was created in the C function and printed in binary. In my Python code I am at the appropriate line of the file to read in the struct. I have tried both unpacking the stuct item by item and as a whole without success. Most of the items in the struct were declared 'real' in the C code. I am working on this code with someone else and the main source code is his and has declared the variables as 'real'. I need to put this in a loop because I want to read all of the files in the directory that end in '.dat'. To start the loop I have:
for files in os.listdir(path):
if files.endswith(".dat"):
part = open(path + files, "rb")
for line in part:
然后我看了所有的线previous加入包含结构之一。然后我去该行并有:
Which then I read all of the lines previous to the one containing the struct. Then I get to that line and have:
part_struct = part.readline()
r = struct.unpack('<d8', part_struct[0])
我想只是读取存储在结构的第一件事。我看到这个地方就在这里的一个例子。当我尝试这个我收到一条错误:
I'm trying to just read the first thing stored in the struct. I saw an example of this somewhere on here. And when I try this I'm getting an error that reads:
struct.error: repeat count given without format specifier
我将采取任何及所有的提示有人可以给我。我一直停留在这几天,并尝试了许多不同的事情。说实话,我觉得我不理解的结构模块,但我读过,就像我可以在上面。
I will take any and all tips someone can give me. I have been stuck on this for a few days and have tried many different things. To be honest, I think I don't understand the struct module but I've read as much as I could on it.
谢谢!
推荐答案
您可以使用的 ctypes.Structure
或的 struct.Struct
指定文件的格式。要通过 C $ C $℃的@ perreal的回答产生的文件中读取结构:
You could use ctypes.Structure
or struct.Struct
to specify format of the file. To read structures from the file produced by C code in @perreal's answer:
"""
struct { double v; int t; char c;};
"""
from ctypes import *
class YourStruct(Structure):
_fields_ = [('v', c_double),
('t', c_int),
('c', c_char)]
with open('c_structs.bin', 'rb') as file:
result = []
x = YourStruct()
while file.readinto(x) == sizeof(x):
result.append((x.v, x.t, x.c))
print(result)
# -> [(12.100000381469727, 17, 's'), (12.100000381469727, 17, 's'), ...]
请参阅 io.BufferedIOBase.readinto ()
。它在Python 3的支持,但它是在Python 2.7无证的默认的文件对象。
struct.Struct
要求指定填充字节( X
)明确:
struct.Struct
requires to specify padding bytes (x
) explicitly:
"""
struct { double v; int t; char c;};
"""
from struct import Struct
x = Struct('dicxxx')
with open('c_structs.bin', 'rb') as file:
result = []
while True:
buf = file.read(x.size)
if len(buf) != x.size:
break
result.append(x.unpack_from(buf))
print(result)
这产生相同的输出。
It produces the same output.
要避免不必要的复制 阵列。 from_buffer(mmap_file)
可以用来从一个文件中获得结构的数组:
To avoid unnecessary copying Array.from_buffer(mmap_file)
could be used to get an array of structs from a file:
import mmap # Unix, Windows
from contextlib import closing
with open('c_structs.bin', 'rb') as file:
with closing(mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_COPY)) as mm:
result = (YourStruct * 3).from_buffer(mm) # without copying
print("\n".join(map("{0.v} {0.t} {0.c}".format, result)))
这篇关于从在c结构阅读蟒蛇结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!