Python 在SQLalchemy中压缩行和列?
我正在将我的库从使用pyodbc转换为SQLalchemy。不幸的是,我遇到了以下错误: TypeError(“不可损坏的类型:'列表'”) 以下是我的功能:Python 在SQLalchemy中压缩行和列?,python,sqlalchemy,typeerror,pyodbc,fetchall,Python,Sqlalchemy,Typeerror,Pyodbc,Fetchall,我正在将我的库从使用pyodbc转换为SQLalchemy。不幸的是,我遇到了以下错误: TypeError(“不可损坏的类型:'列表'”) 以下是我的功能: @route('/api/query/<query>') # not a public system! def qry(query): # `conn` is constructed with `create_engine` last_query = conn.execute(query) retur
@route('/api/query/<query>') # not a public system!
def qry(query):
# `conn` is constructed with `create_engine`
last_query = conn.execute(query)
return dict(result=[{zip(last_query._metadata.keys, row)}
for row in last_query.fetchall()])
爆炸的部分是
{zip(last_query._metadata.keys, row)}
问题是{}
语法不会自动将元组列表转换为字典。如果你换成
dict(zip(last_query._metadata.keys, row))
你该走了
下面是python shell中的一个简化示例:
>>> a = ['a', 'b']
>>> b = [1, 2]
>>> {zip(a, b)}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> dict(zip(a, b))
{'a': 1, 'b': 2}
>a=['a','b']
>>>b=[1,2]
>>>{zip(a,b)}
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:不可损坏的类型:“列表”
>>>dict(zip(a,b))
{'a':1,'b':2}
爆炸的部分是
{zip(last_query._metadata.keys, row)}
问题是{}
语法不会自动将元组列表转换为字典。如果你换成
dict(zip(last_query._metadata.keys, row))
你该走了
下面是python shell中的一个简化示例:
>>> a = ['a', 'b']
>>> b = [1, 2]
>>> {zip(a, b)}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> dict(zip(a, b))
{'a': 1, 'b': 2}
>a=['a','b']
>>>b=[1,2]
>>>{zip(a,b)}
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:不可损坏的类型:“列表”
>>>dict(zip(a,b))
{'a':1,'b':2}