[SciPy-user] [Fwd: Re: storing numpy arrays]
Michael Hearne
mhearne at usgs.gov
Fri Mar 13 12:53:59 EDT 2009
All: Never mind - I figured out I needed to seek to the beginning of the
file-like object I created!
--Mike
-------- Original Message --------
Subject: Re: [SciPy-user] storing numpy arrays
Date: Fri, 13 Mar 2009 10:33:30 -0600
From: Michael Hearne <mhearne at usgs.gov>
Reply-To: SciPy Users List <scipy-user at scipy.org>
To: SciPy Users List <scipy-user at scipy.org>
References: <49B9802E.9050308 at usgs.gov> <49B991A0.7060409 at usgs.gov>
<3d375d730903121557q573b1d39ud0d631324bc99b18 at mail.gmail.com>
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 at 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.
>
>
_______________________________________________
SciPy-user mailing list
SciPy-user at scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user
More information about the SciPy-User
mailing list