[Tutor] reading binary files
jadrifter
jadrifter at gmail.com
Mon Feb 2 14:24:57 CET 2009
On Mon, 2009-02-02 at 11:31 +0000, etrade.griffiths at dsl.pipex.com wrote:
> Hi
>
> I am trying to read data from a file that has format
>
> item_name num_items item_type items ....
>
> eg
>
> TIME 1 0.0
> DISTANCE 10 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
> TIME 1 1.0
> DISTANCE 10 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
>
> I can read this if the data are in ASCII format using
>
> in_file = open("my_file.dat","r")
> data1 = in_file.read()
> tokens = data1.split()
>
> and then stepping through the resulting list but the data
> also appear in the same format in a binary file. I tried
> converting the binary file to an ASCII file using
>
> ifile = open("my_file.dat","rb")
> ofile = open("new_file.dat","w")
> base64.decode(ifile, ofile)
>
> but that gave the error "Error: Incorrect padding". I imagine
> that there is a straightforward way of doing this but haven't
> found it so far. Would be grateful for any suggestions!
>
> Thanks
>
> Alun Griffiths
>
Honestly I'm not sure what you're asking for but in general for reading
binary data the I use the struct module. Check it out in the
documentation.
John Purser
More information about the Tutor
mailing list