Re: Read long int from binary file

From: Enrico Morelli (enrico_morelli_at_yahoo.com)
Date: 10/15/03


Date: Wed, 15 Oct 2003 11:40:01 +0200

On Wed, 15 Oct 2003 08:06:56 +0000, Alex Martelli wrote:

> Enrico Morelli wrote:
> ...
>> I do know the format (in advance).
>> I have some binary files coming from SGI boxes and other from Linux boxes.
>> These files contains 32 bit long integer data that I need to read and
>> display in some graphic format.
>> In some cases I have to reverse the byte order in other not.
>>
>> I'm unable to read these files and put the data in some array.
>> I tried to use f.read(4), but I have 4 numbers not one.
>
>
> import array
>
> x1 = array('l')
> f1 = file('file_ok.dat', 'rb')
> x1.fromfile(f1)
> f1.close()
>
> x2 = array('l')
> f2 = file('file_toswap.dat', 'rb')
> x2.fromfile(f2)
> x2.byteswap()
> f2.close()
>
>
> Alex

Thanks Alex!!!

A question, the fromfile syntax wants the n items to read.
x1.fromfile(f1,1) read exactly one 32 bit long integer?

Enrico

PS. Sei sempre 'r mejo!!



Relevant Pages

  • Re: Read long int from binary file
    ... > I do know the format. ... > I have some binary files coming from SGI boxes and other from Linux boxes. ... > These files contains 32 bit long integer data that I need to read and ... > I'm unable to read these files and put the data in some array. ...
    (comp.lang.python)
  • Re: A Question about arrays file merging
    ... > I do have some programing experience-mostly working ... and in both cases load the output into an array? ... format you can use either language's I/O facilities [e.g. 'fopen', ... then I understand the portability concern. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Raw image in header file
    ... Such things don't belong in headers. ... initialiser for an array. ... Or, you could use a file format that, itself, is C code. ... Here is the complete contents of an .xpm file, ...
    (comp.lang.c)
  • Re: opening multiple files in fortran
    ... last format: r_hifreq10000.out ... array, outnu. ... - so each of my 10,000 output files would have 14,391 entries apiece). ... you are truly reading a large number of files, ...
    (comp.lang.fortran)
  • Re: How do I use the unpack function?
    ... I'm getting streaming tile data from a renderer in order to allow the user to see the rendering of tiles in real-time to create the total image. ... The data is raw bytes and can be any format. ... All data is a string of bytes, and interpreting that string of bytes to have some particular form is applying a format to it. ... Here's a hint on how to use numpy for decoding a byte string into an array of floats. ...
    (comp.lang.python)