[Numpy-discussion] fast constructor for arrays from byte data (unpickling?)

Chris Barker chrishbarker at home.net
Tue Aug 7 16:26:57 EDT 2001


Hung Jung Lu wrote:
> Arrays have a method called tostring() which generates
> the binary data. Is there an inverse function to that?
> That is, generating an array from the binary data
> string?

As you suspected, there is such a thing, and it is called (surprise):
fromstring(). It is a function, rather than a method, as it is a
constructor for an array. To get data from a file, you have to get it
into a string first, so I use:

M = fromstring(file.read(),Float)

This makes a rank -1 array. You might have to reshape it.

Note that you end up creating a Python string of the data first, and
then a NumPy array from that. This doesn't really cost that much, but it
can be an issue with huge data sets. I wish there was a fromfile()
function. I may get around to writing it one day.

-Chris







-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------




More information about the NumPy-Discussion mailing list