Hello i am trying to create a dictionary that would like this
{104: {'tid': 1234, 'date': '08/26/2022', 'total': '95.96'}, {'tid': 1235, 'date': '09/25/2022', 'total': '95.96'}, {'tid': 1236, 'date': '07/27/2022', 'total': '95.96'}}
{105: {'tid': 1237, 'date': '08/26/2022', 'total': '85.96'}, {'tid': 1238, 'date': '09/25/2022', 'total': '85.96'}, {'tid': 1238, 'date': '07/27/2022', 'total': '85.96'}}
I have the following in a db which i am querying
CID || TID || DATE || TOTAL
104 1234 08/26/2022 95.96
104 1235 09/25/2022 95.96
104 1236 07/27/2022 95.96
105 1237 08/26/2022 85.96
105 1238 09/25/2022 85.96
105 1239 07/27/2022 85.96
i have the following code in order to loop through the query results and create a dictionary but its not creating it like the i am wanting it o
outer_dict = {}
records = cursor.fetchall()
for i in records:
inner_dict = {"tid":i[1], "date": i[2], "total": i[3]}
outer_dict[i[0]] = inner_dict
Any help would be very much appreciated.
source https://stackoverflow.com/questions/74437708/python-nested-dictionaries-from-sql-queries
Comments
Post a Comment