Tuesday 15 July 2014

reading struct in python from created struct in c -


I am very new to using Python and am very confused with C, so how do I mouth and / or Lost sound.

I have a function in C that creates a .dat file with data. I am opening the file via Python to read the file. One of the things I need to read is a structure that was made in the Si function and was printed in binary. In my Python Code I am on the proper line of file to read in the structure. I have tried to remove all the accessories of stuff and without success, depending on the items they have. Most of the objects in the structure were declared 'real' in the C code. I am working on this code with someone else and the main source code is that and it has declared the variable as 'real', I have to put it in a loop because all the ones that end in '.dat' I want to read the files in the directory. I have to start the loop:

  for files in oslistdir (path): if files.endswith (".dat"): part = open (path + files, "rb") For the queue in part:   

Then I read all rows before the one with the structure. Then I go to that line and I have:

  part_struct = part.readline () r = struct.unpack ('& lt; d8', part_struct [0])   

I'm just trying to read the first thing stored in the struct I have seen an example of this somewhere

I will take any other tips Someone can give me some I have been stuck on this for a few days and have tried many different things. To be honest, I think I can not understand the composition module, but I have read as much as I have read.

Thank you!

You can use or specify the format of the file. To read the structures from the file generated by:

  "" "structure {double v; int c; char c;}" "" "Ctypes file as imported ('c_structs.bin', 'rb') with import * class YourStruct (structure): _fields_ = [('v', c_double), ('t', c_int), ('c ', C_char)]: result = [] x = YourStruct () while file.readinto (x) == shape (x): result.append ((xv, xt, xc)) print (result) # - & gt; See [(12.100000381469727, 17, 's'), (12.100000381469727, 17, 's'), ...]   

It is supported in Python 3, but it is in Python 2.7 Has been unsuited.

struct.structure It is necessary to clearly specify the padding byte ( x ):

 struct {double v; int t; char c;} ; "" "Structure x = Struct ('dicxxx') from the structure import as the file ('c_structs.bin', 'rb'): result = [] while true: buf = file.read (x .size) if len (buf)! = X.size: break result.append (X.unpack_from (buf)) Print (result)   

This produces the same output.

To avoid unnecessary copy, import import from contextlib import with the file as open ('c_structs.bin', 'rb') as a file:

  Import mmap # Unix, (Mmap.mmap (file.fileno (), 0, access = mmap.ACCESS_COPY)) mm: Result = (YourStruct * 3 ) .from_buffer (mm) # without print copy ("\ n" {0.v} {0}} {0} format, result))    < / Html>

No comments:

Post a Comment