【问题标题】:how to convert buffer read from sqlite3 to bitarrary如何将从 sqlite3 读取的缓冲区转换为二进制
【发布时间】:2015-12-01 04:13:03
【问题描述】:

代码:

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'

【问题讨论】:

    标签: python sqlite bitarray


    【解决方案1】:

    刚刚发现

     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
    

    【讨论】:

    • 亚历山大:这是他自己问题的答案。
    猜你喜欢
    • 2016-09-16
    • 2014-02-21
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多