reading float from binary data file

Discussion in 'Python' started by cesco, Mar 8, 2006.

  1. cesco

    cesco Guest

    Hi,

    I have a binary file containing 1000 floating point numbers. I want to
    load that file into an array. A way to do it could be the following:

    >>> import array
    >>> data = array.array('f')
    >>> f = open('FileName.bin', 'rb')
    >>> data.fromfile(f, 1000)


    Now I have the following problem: if I don't know how many values the
    file contains and I want to read all the values till the last one what
    shall I do?

    Thanks & regards
    Francesco
     
    cesco, Mar 8, 2006
    #1
    1. Advertising

  2. cesco schrieb:
    > Hi,
    >
    > I have a binary file containing 1000 floating point numbers. I want to
    > load that file into an array. A way to do it could be the following:
    >
    >>>> import array
    >>>> data = array.array('f')
    >>>> f = open('FileName.bin', 'rb')
    >>>> data.fromfile(f, 1000)

    >
    > Now I have the following problem: if I don't know how many values the
    > file contains and I want to read all the values till the last one what
    > shall I do?


    Divide the length of the file by the size of one float and use that as
    argument to fromfile.

    Diez
     
    Diez B. Roggisch, Mar 8, 2006
    #2
    1. Advertising

  3. cesco

    Peter Hansen Guest

    cesco wrote:
    > I have a binary file containing 1000 floating point numbers. I want to
    > load that file into an array. A way to do it could be the following:
    >
    >>>>import array
    >>>>data = array.array('f')
    >>>>f = open('FileName.bin', 'rb')
    >>>>data.fromfile(f, 1000)

    >
    > Now I have the following problem: if I don't know how many values the
    > file contains and I want to read all the values till the last one what
    > shall I do?


    Maybe RTFM, and then wrap the .fromfile() call with an appropriate
    exception handler:

    '''
    fromfile(f, n)
    Read n items (as machine values) from the file object f and append
    them to the end of the array.

    If less than n items are available, EOFError is raised, but the items
    that were available are still inserted into the array.
    '''

    Note the last sentence above.

    -Peter
     
    Peter Hansen, Mar 9, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bd
    Replies:
    0
    Views:
    651
  2. Kutty Banerjee

    float (*Data)[4] vs float *Data[4]

    Kutty Banerjee, Mar 3, 2004, in forum: C Programming
    Replies:
    5
    Views:
    347
    mehul raval
    Mar 4, 2004
  3. itdevries
    Replies:
    17
    Views:
    4,333
    James Kanze
    May 31, 2008
  4. Carsten Fuchs
    Replies:
    45
    Views:
    1,607
    James Kanze
    Oct 8, 2009
  5. someone
    Replies:
    37
    Views:
    2,580
    Joshua Maurice
    Oct 18, 2011
Loading...

Share This Page