Код: Выделить всё
import psycopg2
# establishing the connection
conn = psycopg2.connect(
database="CONNE py",
user='postgres',
password='asdf',
host='localhost',
port= '5432'
)
cursor=conn.cursor()
tables = ["Class_A", "Class_B", "Class_C"]
outer_dict={}
for table in tables:
cursor.execute(f'SELECT name, department, overall_percentage FROM {table}')
rows=cursor.fetchall()
inner_dict={}
for row in rows:
name, department, overall_percentage=row
inner_dict={
'name':name,
'department':department,
'overall_percentage':overall_percentage
}
outer_dict[table]=inner_dict
print(outer_dict)
cursor.close()
это выходные данные:
Код: Выделить всё
{'Class_A': {'name': 'Ranga', 'department': 'MCA', 'overall_percentage': 72}, 'Class_B': {'name': 'Delli', 'department': 'MBA', 'overall_percentage': 62}, 'Class_C': {'name': 'Mohan', 'department': 'B.tech AI', 'overall_percentage': 92}}
Код: Выделить всё
{'Class_A': {{'name': 'Ranga', 'department': 'MCA', 'overall_percentage': 72}, {'name': 'Ranga', 'department': 'MCA', 'overall_percentage': 72}, {'name': 'Ranga', 'department': 'MCA', 'overall_percentage': 72}, }