我试图从我的数据库中提取数据并将其放入可以在 chart.js 中显示的格式。我使用 python,我的数据库使用 sqlite3。我设法从表中提取必要的值并以表格格式打印出来进行检查,但我不确定从这里去哪里。from os import mkdirimport sqlite3try: conn = sqlite3.connect('foldername/db.sqlite')except sqlite3.OperationalError: mkdir('foldername')finally: conn = sqlite3.connect('foldername/db.sqlite')cursor = conn.cursor()room_no = 1cursor.execute( "SELECT room, strftime( '%H:00', date ) AS HOUR, SUM(count) AS DENSITY FROM crowd WHERE strftime('%Y-%m-%d', date) = strftime('%Y-%m-%d','now') AND room = ? group by HOUR order by HOUR asc",(room_no,))for row in cursor: print('{0} : {1}, {2}'.format(row[0], row[1], row[2]))cursor.close()conn.close()为了澄清,输出当前打印如下:1 : 09:00, 31 : 10:00, 41 : 11:00, -21 : 12:00, 11 : 13:00, 11 : 14:00, 1但我需要将不同的列放入这样的数组中:['09:00','10:00','11:00',...][3, 4, -2,...]有什么建议么?
1 回答
小怪兽爱吃肉
TA贡献1852条经验 获得超1个赞
您可以zip(*...)
在 python 中使用transpose
您的数据。
cursor.execute("SELECT room, strftime( '%H:00', date ) AS HOUR, SUM(count) AS DENSITY FROM crowd WHERE strftime('%Y-%m-%d', date) = strftime('%Y-%m-%d','now') AND room = ? group by HOUR order by HOUR asc",(room_no,))
data = cursor.fetchall()
data = list(zip(*data))
data[1]然后将是['09:00','10:00','11:00',...]((data[0])房间)
添加回答
举报
0/150
提交
取消