reading float from binary data file

C

cesco

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:

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
 
D

Diez B. Roggisch

cesco said:
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:


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
 
P

Peter Hansen

cesco said:
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:


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top