Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a raw binary file with NumPy array data

I'd like to save the contents of a numpy float array into a raw binary file as signed 16 bit integers. I tried to accomplish this using ndarray.tofile but I can't figure out the right format string. It seems that the file is saved in double format, mo matter how I choose the format string. How do I do this? Thanks.

like image 843
Peter Avatar asked May 10 '12 14:05

Peter


People also ask

How do I save a NumPy array to a text file?

Let us see how to save a numpy array to a text file. Creating a text file using the in-built open() function and then converting the array into string and writing it into the text file using the write() function. Finally closing the file using close() function.

What is a raw binary file?

A binary file (with the extension . rbf) containing configuration data for use outside the Quartus® Prime software. A Raw Binary File contains the binary equivalent of a Tabular Text File (. ttf).


1 Answers

I think the easiest way to do this is to first convert the array to int16,

array.astype('int16').tofile(filename) 
like image 144
Bi Rico Avatar answered Sep 19 '22 14:09

Bi Rico