473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

reading float from binary data file

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

Mar 8 '06 #1
2 14221
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
Mar 8 '06 #2
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

Mar 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Kevin Knorpp | last post by:
Hello. I need to be able to extract the data from the attached file (or any file in the same format) so that I can work with the data in PHP. I'm fairly comfortable with using PHP with...
7
by: Daniel Moree | last post by:
I'm working on a program that must first establish if the file exists in the program directory then it must open if for reading, read each line and set the variables then the program goes on about...
8
by: Darsant | last post by:
I'm currently reading 1-n number of binary files, each with 3 different arrays of floats containing about 10,000 values a piece for a total of about 30,000 values per file. I'm looking for a way...
7
by: laclac01 | last post by:
So I am converting some matlab code to C++. I am stuck at one part of the code. The matlab code uses fread() to read in to a vector a file. It's a binary file. The vector is made up of floats,...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
2
by: Ady | last post by:
Hi, I am trying to create an editor and the structure of our existing files are as follows. In C# how would I read the following binary data from a file and convert it into usable data. //...
2
by: Matt McGonigle | last post by:
Hi all, Please help me out with this. Perhaps it is a dumb question, but I can't seem to make it work. I am doing a file conversion using an unformatted binary file for input and outputting to...
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
31
by: tophandasa | last post by:
Hi all, I'm having a trouble reading a binary file as float values.I have to read the data in binary mode, then read every four bytes into a float variable. I have done my search, but i found out...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.