Reading binary with python

It is especially difficult for me to read the binary with Python. Can you give me a hand? I need to read this file, which is easy to read in Fortran 90

int*4 n_particles, n_groups real*4 group_id(n_particles) read (*) n_particles, n_groups read (*) (group_id(j),j=1,n_particles) 

Detail file format:

 Bytes 1-4 -- The integer 8. Bytes 5-8 -- The number of particles, N. Bytes 9-12 -- The number of groups. Bytes 13-16 -- The integer 8. Bytes 17-20 -- The integer 4*N. Next many bytes -- The group ID numbers for all the particles. Last 4 bytes -- The integer 4*N. 

How can I read this using Python? I tried everything, but it never worked. Is there a chance that I can use the f90 program in python while reading this binary and then save the data I need to use?

+57
python fortran binary
Jan 03 2018-12-12T00:
source share
4 answers

Read the contents of the binary as follows:

 with open(fileName, mode='rb') as file: # b is important -> binary fileContent = file.read() 

then โ€œunzipโ€ the binary data using struct.unpack :

Starting bytes: struct.unpack("iiiii", fileContent[:20])

Body: ignore header bytes and trailing bytes (= 24); The remainder forms the body to find out how many bytes in the body are integer divisible by 4; The resulting coefficient is multiplied by the string 'i' to create the correct format for the decompression method:

 struct.unpack("i" * ((len(fileContent) -24) // 4), fileContent[20:-4]) 

End byte: struct.unpack("i", fileContent[-4:])

+75
Jan 03 2018-12-12T00:
source share

In general, I would recommend that you learn the Python struct for this. This is standard with Python, and it should be easy to translate the question specification into a format string suitable for struct.unpack() .

Note that if there is an โ€œinvisibleโ€ padding between the "around" fields, you will need to figure this out and include it in the unpack() call, or you will read the wrong bits.

Reading the contents of the file in order to unzip something was pretty trivial:

 import struct data = open("from_fortran.bin", "rb").read() (eight, N) = struct.unpack("@II", data) 

This unpacks the first two fields, assuming that they start from the very beginning of the file (without filling or extraneous data), and also taking into account their own byte order ( @ symbol). I in the format string means "unsigned integer, 32 bits".

+20
Jan 03 '12 at 10:18
source share

You can use numpy.fromfile , which can read data from text and binary files. First you create a data type that represents your file format using numpy.dtype , and then read that type from the file using numpy.fromfile .

+8
Jan 03 '12 at 10:41
source share
 import pickle f=open("filename.dat","rb") try: while True: x=pickle.load(f) print x except EOFError: pass f.close() 
-one
Dec 13 '17 at 16:26
source share



All Articles