Read header and data from a binary file [LONG]

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Sep 25 04:47:39 EDT 2009


En Tue, 22 Sep 2009 18:18:16 -0300, Jose Rafael Pacheco  
<jose_rafael_pacheco at yahoo.es> escribió:

> Hello,
>
> I want to read from a binary file called myaudio.dat
> Then I've tried the next code:
>
> import struct
> name = "myaudio.dat"
> f = open(name,'rb')
> f.seek(0)
> chain = "< 4s 4s I 4s I 20s I I i 4s I 67s s 4s I"
> s = f.read(4*1+4*1+4*1+4*1+4*1+20*1+4*1+4*1+4*1+4*1+4*1+67*1+1+4*1+4*1)

> a = struct.unpack(chain, s)

Easier:
fmt = struct.Struct(chain)
s = f.read(fmt.size)
a = fmt.unpack(s)

> The audio data length is 300126, now I need a clue to build an array with
> the audio data (The Chunk SDA_), would it possible with struct?, any  
> help ?

The chunk module (http://docs.python.org/library/chunk.html) is designed  
to work with such file format.

-- 
Gabriel Genellina




More information about the Python-list mailing list