作者hl4 (Minimi)
看板Python
标题[问题] 如何将 class buffer 转换成 list
时间Thu Jun 5 19:32:43 2014
目的是将int list,存入sqlite3的资料库的一个栏位中,在之後读出回复为int list。
但是在读取之後,不知道应该怎麽将位元矩阵恢复为4-byte为单位的整数资料
写入的过程如下:
# 建立资料库和数值初始化
c.execute('''CREATE TABLE IF NOT EXISTS Log (id INTEGER PRIMARY KEY,
tag TEXT, data BLOB)''')
tmp = [1, 2, 3, 4]
# int list 转换为 buffer
a = array.array('I', tmp)
b = buffer(a.tostring())
# 写入新资料
c.execute('''INSERT INTO Log(tag, data) VALUES ('R1', ?)''', (b,))
conn.commit()
目前的读取方式:
for row in c.execute('''SELECT * FROM Log'''):
print bytearray(row[2])[0]
print bytearray(row[2])[1]
print bytearray(row[2])[2]
然後就看到原本的4-byte的数值被切成1-byte,不知道应该怎麽恢复。
虽然我也可以自己加乘起来,但是这貌似不是这样设计的,就来po文问问。
谢谢。
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 59.124.197.149
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Python/M.1401967970.A.7E5.html