Re: Reading binary data from Postgres 9

Поиск
Список
Период
Сортировка
От Daniele Varrazzo
Тема Re: Reading binary data from Postgres 9
Дата
Msg-id CA+mi_8aic757K4RJ7qwBJ3X+kHGFYiOPdW4h0f23bYF-JbCQCg@mail.gmail.com
обсуждение исходный текст
Ответ на Reading binary data from Postgres 9  (Ian <youknowho2000@yahoo.com>)
Ответы Re: Reading binary data from Postgres 9
Список psycopg
On Fri, Aug 19, 2011 at 1:28 AM, Ian <youknowho2000@yahoo.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

В списке psycopg по дате отправления:

Предыдущее
От: Ian
Дата:
Сообщение: Reading binary data from Postgres 9
Следующее
От: Ian
Дата:
Сообщение: Re: Reading binary data from Postgres 9