[SciPy-user] storing numpy arrays

Michael Hearne mhearne@usgs....
Fri Mar 13 11:33:30 CDT 2009


Robert - Thanks.  I hadn't run across those methods.

However...

I tried the script below (using numpy version "1.1.0.dev5077"), and got 
the following output:

####output####
[ 0.05867671  0.71670412  0.99391546  0.02065827]
?NUMPYF{'descr': '<f8', 'fortran_order': False, 'shape': (4,)}             
pm???
??җ{=????=??'??? 9m?p'??
Traceback (most recent call last):
  File "./testme.py", line 13, in <module>
    y = numpy.lib.format.read_array(output)
  File 
"/Library/Python/2.5/site-packages/numpy-1.1.0.dev5077-py2.5-macosx-10.3-i386.egg/numpy/lib/format.py", 
line 302, in read_array
    version = read_magic(fp)
  File 
"/Library/Python/2.5/site-packages/numpy-1.1.0.dev5077-py2.5-macosx-10.3-i386.egg/numpy/lib/format.py", 
line 105, in read_magic
    raise ValueError("could not read %d characters for the magic string; 
got %r" % (MAGIC_LEN, magic_str))
ValueError: could not read 8 characters for the magic string; got ''
####output####

######script#####
#!/usr/bin/python

from numpy import array
from numpy.random import random
import numpy.lib.format
import StringIO

output = StringIO.StringIO()
x = random(4)
print x
numpy.lib.format.write_array(output, x)
print output.getvalue()
y = numpy.lib.format.read_array(output)
print y
######script#####

Robert Kern wrote:
> On Thu, Mar 12, 2009 at 17:50, Michael Hearne <mhearne@usgs.gov> wrote:
>   
>> Conor - Thank you for your suggestion.  I am in fact already using
>> pytables in another part of my application.  However, using PyTables for
>> this portion is not a desirable option.
>>
>> Perhaps I should rephrase the question:
>> What is the the most efficient way to serialize a numpy array into a
>> structure that _could_ be written to a disk, or stuffed into a database,
>> etc?
>>     
>
> Use numpy.lib.format.{read,write}_array() to use the NPY format, which
> should be the most robust and efficient way to do what you want. I
> have used this to store numpy arrays as MySQL BLOBs before, and it
> worked quite well.
>
>   



More information about the SciPy-user mailing list