Writing formatted data (numeric) to a binary file

Robert Kern rkern at ucsd.edu
Fri Sep 2 00:09:22 EDT 2005


Trevor.Dhu at 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
rkern at ucsd.edu

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




More information about the Python-list mailing list