arynaq
arynaq

Reputation: 6870

How can I read and write binary files?

I don't have access to a PC for the next couple of days but I can't get this problem off my mind. I am just playing around with compression algorithms, created one of my own for audio and I am stuck at the output to file step. So here are my questions, hopefully I can figure out an answer before I am back or else this will eat my mind.

1) If I have a numpy X array with some integers (say int16), if I open a file object and do file.write(X) what will the file output be like? Numbers? Or ASCII of numbers? Or binary?

2) Depending on the above answer, how do I read this file into a numpy array X?

Essentially my compression does some wavelet and fft transforms, does some filtering here and there and returns an array with some numbers, I know the format of this array and I already achieve a high % of compression here, the next step is to first dump this array into a binary file. Once I achieve this my next goal is to implement some kind of entropy coding of the file/vector.

Any input appreciated.

Upvotes: 2

Views: 6008

Answers (1)

Bula
Bula

Reputation: 1586

1) Writing:

In [1]: f = open('ints','wb')
In [2]: x = numpy.int16(array([1,2,3]))
Out[2]: array([1, 2, 3], dtype=int16)
In [3]: f.write(x)
In [4]: f.close()

2) Reading:

In [5]: f = open('ints','wb')
In [6]: x = f.read()
In [7]: x
Out[7]: '\x01\x00\x02\x00\x03\x00'
In [8]: numpy.fromstring(x, dtype=np.uint16, count=3)
Out[8]: array([1, 2, 3], dtype=uint16)

Update:

As J.F.Sebastian suggested there are better ways to do this, like using:

or as Janne Karila suggested using:

Upvotes: 4

Related Questions