我想在列表中获取fetchall操作的结果,而不是元组的tuple或字典的元组。例如,
cursor = connection.cursor() #Cursor could be a normal cursor or dict cursor query = "Select id from bs" cursor.execute(query) row = cursor.fetchall()
现在,问题是结果行是(((123,),(234,))或({‘id’:123},{‘id’:234})我正在寻找的是(123,234)或[ 123,234]。如果可以保存解析结果,那就最好了。提前致谢
那么列表理解呢?如果结果为((123,), (234,), (345,)):
((123,), (234,), (345,))
>>> row = [item[0] for item in cursor.fetchall()] >>> row [123, 234, 345]
如果结果为({'id': 123}, {'id': 234}, {'id': 345}):
({'id': 123}, {'id': 234}, {'id': 345}):
>>> row = [item['id'] for item in cursor.fetchall()] >>> row [123, 234, 345]