我在将查询结果转换为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'}
for row in cursor
代替cursor.fetchall()
并获得相同的结果。 - Max Heiberitertools.izip
在Python 3中不可用,但是普通的zip
函数仍然可以使用。 - BanzaiTokyodesc = cur.description cols = [col[0] for col in desc] data = [dict(itertools.zip(cols, row)) for row in cur.fetchall()]
但是我仍然收到以下错误:AttributeError: module 'itertools' has no attribute 'zip'
我想这就是Ri1a试图指出的问题。 - techele