【发布时间】:2015-01-15 19:47:46
【问题描述】:
如何转换
cursor.execute("SELECT strftime('%m.%d.%Y %H:%M:%S', timestamp, 'localtime'), temp FROM data WHERE timestamp>datetime('now','-1 hours')")
# fetch all or one we'll go for all.
results = cursor.fetchall()
for row in results[:-1]:
row=results[-1]
rowstr="['{0}',{1}]\n".format(str(row[0]),str(row[1]))
temp_chart_table+=rowstr
结果
['01.15.2015 21:38:52',21.812]
以下列形式输入字典输出:
[{timestamp:'01.15.2015 21:38:52',temp:21.812}]
编辑
这是我目前使用的 fetchone 示例,它工作正常:
def get_avg():
conn=sqlite3.connect(dbname)
curs=conn.cursor()
curs.execute("SELECT ROUND(avg(temp), 2.2) FROM data WHERE timestamp>datetime('now','-1 hour') AND timestamp<=datetime('now')")
rowavg=curs.fetchone()
#print rowavg
#rowstrmin=format(str(rowavg[0]))
#return rowstrmin
**d = [{"avg":rowavg[0]}]**
return d
conn.close()
#print get_avg()
schema = {"avg": ("number", "avg")}
data = get_avg()
# Loading it into gviz_api.DataTable
data_table = gviz_api.DataTable(schema)
data_table.LoadData(data)
json = data_table.ToJSon()
#print results
#print "Content-type: application/json\n\n"
print "Content-type: application/json"
print
print json
然后我进行 jQuery 调用并将其传递给 javascript,并在此处找到了帮助 ajax json query directly to python generated html gets undefined
【问题讨论】:
-
那个结果好像不对,不应该是
[['01.15.2015 21:38:52',21.812]](双[,意思是列表列表)
标签: python string list dictionary