当我使用session.query时,我能够将结果转换为字典列表:

my_query = session.query(table1,table2).filter(all_filters)
result_dict = [u.__dict__ for u in my_query.all()]

但是现在我必须使用SELECT()操作,对于每行结果,如何将结果转换为看起来像的dict:
[{'Row1column1Name' : 'Row1olumn1Value', 'Row1column2Name' :'Row1Column2Value'},{'Row2column1Name' : 'Row2olumn1Value', 'Row2column2Name' : 'Row2Column2Value'},etc....]

这是我的SELECT()代码:
select = select([table1,table2]).where(all_filters)
res = conn.execute(select)
row = res.fetchone() #I have to use fetchone() because the query returns lots of rows
resultset=[]
while row is not None:
    row = res.fetchone()
    resultset.append(row)

print resultset

结果是:
[('value1', 'value2', 'value3', 'value4'),(.....),etc for each row]

我是Python的新手,将不胜感激。

最佳答案

这似乎是一个RowProxy对象。尝试:

row = dict(zip(row.keys(), row))

关于python - SQLAlchemy将SELECT查询结果转换为字典列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19406859/

10-12 14:28
查看更多