Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在SQLalchemy中压缩行和列?_Python_Sqlalchemy_Typeerror_Pyodbc_Fetchall - Fatal编程技术网

Python 在SQLalchemy中压缩行和列?

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

我正在将我的库从使用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)

    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}