Re: Reading binary data from Postgres 9

From: Ian <youknowho2000(at)yahoo(dot)com>
To: "psycopg(at)postgresql(dot)org" <psycopg(at)postgresql(dot)org>
Subject: Re: Reading binary data from Postgres 9
Date: 2011-08-19 16:35:33
Message-ID: 1313771733.32092.YahooMailNeo@web39414.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

Thank you very much for the reply, using numpy.frombuffer works great. I'm using Python 2.7.2, Postgres 9.04, and Psycopg2.4.2 and tested using my_array.tostring() as well. Both with and without my_array.tostring() I get the same numpy array result, no exceptions here. Weird.

Thanks again!
Ian 

>________________________________
>From: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
>To: Ian <youknowho2000(at)yahoo(dot)com>
>Cc: "psycopg(at)postgresql(dot)org" <psycopg(at)postgresql(dot)org>
>Sent: Friday, August 19, 2011 3:40 AM
>Subject: Re: [psycopg] Reading binary data from Postgres 9
>
>On Fri, Aug 19, 2011 at 1:28 AM, Ian <youknowho2000(at)yahoo(dot)com> wrote:
>> Hi list,
>> I am storing a NumPy array in binary form in Postgres with:
>> import numpy as np
>> ...
>> my_array = np.array(data_list, np.float32)
>> cur.execute("insert into my_table values (%s)",
>> (psycopg2.Binary(my_array),))
>
>Are you sure? I get an error with this (can't escape numpy.ndarray to
>binary). Looks like it would be required to use
>psycopg2.Binary(my_array.tostring()). Is it a typo from you or a
>regression? (I'm using 2.4.x).
>
>> I've confirmed that this works fine. However when I query this binary data
>> from the database I am having trouble deserializing/getting it back into a
>> NumPy array. The result comes back as a read-only buffer. Does psycopg2 have
>> an opposite of Binary to handle this?
>
>you can use numpy.frombuffer(b, np.float32) to have your data back,
>where b is the buffer read from the cursor.
>
>Cheers,
>
>-- Daniele
>
>
>

In response to

Browse psycopg by date

  From Date Subject
Next Message David Butler 2011-08-22 14:53:03 backtrace in 2.4.2
Previous Message Daniele Varrazzo 2011-08-19 10:40:53 Re: Reading binary data from Postgres 9