4

我正在使用 cPickle 和 psycopg2 将一些向量存储到数据库中。这是我存储二进制数据的代码

binary_vec = cPickle.dumps(vec, -1)
db.cur.execute('''
               INSERT INTO feature_vector (vector, id) 
               VALUES (%s, %s);
               ''', (psycopg2.Binary(binary_vec), thread_id)
db.conn.commit()

但是,当我使用 fetchall() 加载数据时,类型是缓冲区。我找不到如何将此缓冲区对象恢复回列表(vec)。

这就是我获取数据的方式

db.cur.execute("SELECT * FROM feature_vector;")
m = db.cur.fetchall()

结果看起来像这样

[(3169187, <read-only buffer for 0x1002b0f10, size 3462, offset 0 at 0x1004a7430>), 
(3169275, <read-only buffer for 0x1002b0f50, size 3462, offset 0 at 0x1004a7570>), 
(3169406, <read-only buffer for 0x1002b0f70, size 3462, offset 0 at 0x10140b0b0>), 
(3169541, <read-only buffer for 0x10141c030, size 3462, offset 0 at 0x10140b2b0>), 
(3169622, <read-only buffer for 0x10141c050, size 3462, offset 0 at 0x10140b3f0>),...

当我尝试使用 cPickle.loads(m[0][1]) 时,它会返回错误消息

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be string, not buffer
4

1 回答 1

7

您可以创建一个自定义的类型转换程序来自动将腌制值转换为 Python:

import cPickle
obj = {'a': 10}
data = cPickle.dumps(obj, -1)

import psycopg2

def cast_pickle(data, cur):
    if data is None: return None
    return cPickle.loads(str(psycopg2.BINARY(data, cur)))

psycopg2.extensions.register_type(
    psycopg2.extensions.new_type(
        psycopg2.BINARY.values, 'BINARY-PICKLE', cast_pickle))

cnn = psycopg2.connect('')
cur = cnn.cursor()
cur.execute("select %s::bytea", [psycopg2.Binary(data)])
cur.fetchone()
# ({'a': 10},)
于 2012-12-04T02:19:02.253 回答