[英]How to convert sql query output from list of tuples to a list of dicts in python3 where each dict represents a row in a table?
我的代碼看起來像這樣:
query = """ SELECT col1, col2 FROM table WHERE col3 = value"""
cursor.execute(query)
data = cursor.fetchall()
print(data)
輸出將類似於:
[('col1[1][0]', 'col2[1][0]'), ('col1[2][1]', 'col2[2][1]')]
但我不希望輸出像那樣,我希望它是一個字典列表,其中每一行代表一個包含鍵和值的字典。
所以輸出看起來像:
col1[1][0], col2[1][0]
col1[2][1], col2[2][1]
並且沒有單引號
嘗試:
data =[('A', 1), ('B', 3)]
for a, b in data: # this unpacks the tuple
print(a, b)
A 1
B 3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.