2015-09-04 96 views
1

代碼轉換:如何緩衝從sqlite3的讀取bitarrary

from bitarray import bitarray 
import sqlite3 

a = bitarray('1001011') 

conn = sqlite3.connect(":memory:") 
# conn = sqlite3.connect('tmp.db') 
cursor = conn.cursor() 

cursor.execute("CREATE TABLE t (id INTEGER, x BLOB)") 

cursor.execute("INSERT INTO t values (?, ?)", (1, sqlite3.Binary(a.tobytes()))) 
cursor.execute("select * from t") 

rec = cursor.fetchone() 

conn.commit() 
conn.close() 

b = bitarray.frombytes(rec[1]) 

錯誤:

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
18 conn.close() 
19 
---> 20 b = bitarray.frombytes(rec[1]) 

TypeError: descriptor 'frombytes' requires a 'bitarray._bitarray' object but received a 'buffer' 

回答

0

只是想出了

aa = bitarray() 
aa.frombytes(str(rec[1])) 
print aa 
# bitarray('10010110') 
# Please pay attention to the size, the original bitarray is 1001011, 
# but str convert it to bytes by appending a zero 
+0

亞歷山大:這是回答了自己的問題。 – cfi