3 回答
TA贡献1875条经验 获得超3个赞
您可以将字段名称字典用于行索引。例如:
cursor = [(1, 2, 3), (4, 5, 6)]
fields = { 'date' : 0 , 'servicelevel' : 1, 'power' : 2 }
data_Rows = []
for row in cursor:
data_Rows.append({ f : str(row[v]) for f, v in fields.items() })
print(data_Rows)
输出:
[{'power': '3', 'servicelevel': '2', 'date': '1'}, {'power': '6', 'servicelevel': '5', 'date': '4'}]
TA贡献1835条经验 获得超7个赞
您可以使用每行的列标题来使用构造函数zip为每一行创建一个字典:dict
headers = 'date', 'servicelevel', 'power', 'rebootime', 'result', 'result_two', 'moredata'
data_Rows.extend(dict(zip(headers, map(str, row))) for row in cursor)
TA贡献1836条经验 获得超3个赞
也许是这样:
if cursor:
for row in cursor:
data_Rows.append({'date': str(row[0]), 'servicelevel': str(row[1]), 'power': str(row[2]), 'rebootime': str(row[3]),
'result': str(row[4]), 'result_two': str(row[5]), 'moredata': str(row[6])})
添加回答
举报