[SciPy-User] Write binary file from python and read from C++

Esko Lehtonen esko.lehtonen at gmail.com
Fri Apr 8 17:16:56 EDT 2011


> From: Puiseux<pierre.puiseux at univ-pau.fr>
> I have a simple problem and could not find the solution in scipy/python doc.
> Maybe i did not search in the good place...
>
> I have some numpy.array (float32), and i want to write it in a binary
> file with my own format.
> This file has to be readable  by a C++ program.

With struct module (from Python standard libray) you can "pack" any 
combination of values to strings containing the binary representation 
(you can choose bit-endianess etc.). Then you can write these strings to 
a file.

Myself, I use it otherway round to parse stored UDP packets from. Such 
binary formats are not very user friendly, so I prefer standardized 
formats if possible (like HDF5, see pytables).

- Esko



More information about the SciPy-User mailing list