Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > reading float from binary data file

Reply
Thread Tools

reading float from binary data file

 
 
cesco
Guest
Posts: n/a
 
      03-08-2006
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

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      03-08-2006
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
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      03-09-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reading binary file into memory. Converting from char to uint32,float, double, ASCII strings etc (static_cast< > ?) someone C++ 37 10-18-2011 01:32 AM
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
converting char to float (reading binary data from file) itdevries C++ 17 05-31-2008 08:33 AM
float (*Data)[4] vs float *Data[4] Kutty Banerjee C Programming 5 03-04-2004 11:11 AM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments