473,386 Members | 1,644 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,386 software developers and data experts.

Writing formatted data (numeric) to a binary file

I am just starting to use python, and as such my question may well be a
bit simplistic. I am currently trying to write what should be a very
basic set of routines for reading and writing to a binary grid format
(ERMapper). However:

I am struggling to find the correct tools to write a row of a
2-dimensional array to a binary file. In matlab the command looks
like:

fid=fopen(filename,'wb');
for y=1:NrOfLines
fwrite(fid,in(y,:)','real*4');
end
fclose(fid);

I'm conscious that the "write" command would allow me to write text to
a binary file, but I'm not clear how to:
1) format my numeric data such that it matches the real*4 format
2) parse an entire row of a 2D array without using a for loop.

If anyone has any suggestions on how to do this (or even hints as to
what modules I should be looking at) they would be greatly appreciated.
Alternativly, if anyone has written routines for dealing with basic
ERMapper grids (as opposed to the wavelet compressed data) I'd love to
save myself from re-inventing the wheel.

Thanks
Trevor Dhu

Sep 2 '05 #1
3 2594
On 2005-09-02, Tr********@hotmail.com <Tr********@hotmail.com> wrote:
I'm conscious that the "write" command would allow me to write
text to a binary file, but I'm not clear how to:
1) format my numeric data such that it matches the real*4 format
2) parse an entire row of a 2D array without using a for loop.


http://docs.python.org/lib/module-struct.html

--
Grant Edwards grante Yow! I'm in DISGUISE as a
at BAGGAGE CHECKER....I can
visi.com watch the house, if it's
ORANGE...
Sep 2 '05 #2
Tr********@hotmail.com wrote:
I'm conscious that the "write" command would allow me to write text to
a binary file, but I'm not clear how to:
1) format my numeric data such that it matches the real*4 format
2) parse an entire row of a 2D array without using a for loop.


For homogeneous blocks of numbers like this, you'll probably want to use
Numeric of numarray.

http://numeric.scipy.org

import Numeric
data = Numeric.array(all_of_the_numbers, Numeric.Float32)
s = data.tostring()
f = open(filename, 'wb')
f.write(s)
f.close()

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Sep 2 '05 #3
On 1 Sep 2005 20:39:20 -0700, Tr********@hotmail.com declaimed the
following in comp.lang.python:

<snip>

I'm conscious that the "write" command would allow me to write text to
a binary file, but I'm not clear how to:
1) format my numeric data such that it matches the real*4 format
Look at the struct module
2) parse an entire row of a 2D array without using a for loop.
Suspect you won't be able to avoid it, unless it is a predetermined
length (so you can supply enough struct formats AND destination
variables in one call)
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Sep 2 '05 #4

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

Similar topics

4
by: john smith | last post by:
Hi, I have a file format that is going to contain some parts in ascii, and some parts with raw binary data. Should I open this file with ios::bin or no? For example: filename: a.bin number of...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
2
by: C.H. | last post by:
Hello, I am writing to a .txt file. Lots of data, about 1.1MB. It only took less than a second for VB6 to do so, but it took VB.NET one minute!! I am using a simple fileopen and print. Does...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
5
by: Sheldon | last post by:
Hi everyone, I have a short program the writes the output data to an acsii file: import sys import os import string import gisdata from Numeric import *
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
4
by: daqmaneng | last post by:
does anyone have any suggestions for writing a two dimensional array of data to a file. Currently, I have an array of 5 columns, with 1000 elements per array. I know that I can use a for next...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.