Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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
如何将SQL查询结果转换为python字典_Python_Sql_Dictionary - Fatal编程技术网

如何将SQL查询结果转换为python字典

如何将SQL查询结果转换为python字典,python,sql,dictionary,Python,Sql,Dictionary,我很难将查询结果转换成python字典。每个字典都应该代表一个学生,键是列名,值是查询中对应的值,到目前为止,这就是我得出的结论: def all_students(): qu= 'select * from students' crs.execute(qu) for row in crs: student= {"StudentNum":row[0], "StudentLastName":row[2], "StudentFirst Name":row[3}

我很难将查询结果转换成python字典。每个字典都应该代表一个学生,键是列名,值是查询中对应的值,到目前为止,这就是我得出的结论:

def all_students():
    qu= 'select * from students'
    crs.execute(qu)
    for row in crs:
        student= {"StudentNum":row[0], "StudentLastName":row[2], "StudentFirst Name":row[3}
    return student
但当我打印它时,它返回正确的信息,并且一切都不正常,它只显示一条记录:

{'StudentLastName': Jane, StudentNum: 'Smith  ', StudentFirst Name: '1612'}

您可以使用
cursor.description
获取列名并“压缩”列名列表,每个返回的行生成一个字典列表:

import itertools

desc = cursor.description
column_names = [col[0] for col in desc]
data = [dict(itertools.izip(column_names, row))  
        for row in cursor.fetchall()]

注意:在Python3上使用
zip()
代替
itertools.izip()

此时您可能已经解决了问题,但无论如何:

如果您使用的是mysql,我认为这就是您需要的:

根据定义:MySQLCursorDict游标将每一行作为字典返回。每个dictionary对象的键是MySQL结果的列名

所以你只需要设置 crs=cnx.cursor(dictionary=True)

希望它有帮助

也许这可以帮助:


要回答最初的问题,您可以尝试以下方法(无需循环使用
crs
,您可以直接使用
crs.fetchall()
,因为表内容适合您的内存):

crs.fetchall()中(col1,col2,col3)的dict=[{'StudentNum':col1,'StudentLastName':col2,'StudentFirst Name':col3}的
result\u list\u

dict的结果
result\u list\u是一个字典列表,每个元素(字典)将有您指定的3个键(“StudentNum”、“StudentLastName”和“StudentFirst Name”),以及从SQL表中提取的关联值

扩展:

基本上,
cursor.fetchall()
返回一个元组列表,因此只要列出所有应该返回的值,就可以随意修改前面的部分。例如,以下代码适用于包含5列的表,您希望仅提取前3列:

result_dict={col1:(col2,col3)for cursor.fetchall()中的(col1,col2,col3,col4,col5)}


在这种情况下,生成的
result\u dict
将成为一本字典。

我希望这将有助于

def all_students():
    qu= "select * from students"
    students = crs.execute(qu)
    students_data = []
    for row in students:
        student= {"StudentNum":row[0], "StudentLastName":row[2], "StudentFirst Name":row[3}
         students_data.append(student)

    final_data["students_info"] = students_data
    return final_data

如果您来自哈佛大学的cs50课程,则
SELECT
查询将返回一个列表,其中每一行都由Python
dict
表示,其中键对应于列名,值来自表


您不需要转换它。

对于最后一行,您可以对游标中的行执行
,而不是
cursor.fetchall()
,并获得相同的结果。看起来像
itertools。izip
在Python 3中不可用,simple
zip
可以工作,但在Python 3中内置的zip与2.x中的izip的工作相同(返回一个生成器而不是列表),但速度稍快,因为它是一个内置函数。模块“itertools”没有“zip”member@RickLan当然,为python3添加了一个注释。你能解释一下为什么这样做吗?这段代码在做什么?它是如何回答这个问题的?我做了一些修改,以增加与原始问题的相关性
def all_students():
    qu= "select * from students"
    students = crs.execute(qu)
    students_data = []
    for row in students:
        student= {"StudentNum":row[0], "StudentLastName":row[2], "StudentFirst Name":row[3}
         students_data.append(student)

    final_data["students_info"] = students_data
    return final_data