I am trying to print my nested dictionary in a table format. But have not been able to do so. The table is a list of student names and student grades. I would like it to be in a "Report Card" type format that is easy to read. The for loop I am using prints out the results. But it is one column list form.
This is my current dictionary I have setup.
student = {
"student1": {
"Name": "Eddy",
"Grade": 1,
"Math": 78,
"English": 65,
"Physics": 89,
"Chemistry": 80
},
"student2": {
"Name": "Jim",
"Grade": 2,
"Math": 89,
"English": 65,
"Physics": 87,
"Chemistry": 76
},
"student3": {
"Name": "Jane",
"Grade": 3,
"Math": 87,
"English": 97,
"Physics": 75,
"Chemistry": 64
},
}
This is the for loop I am currently using.
for i in student:
for j in student[i]:
print(j + " : " + str(student[i][j]))
print("===========")
I would like to print out in a table format.
Name Grade Math English Physics Chemistry
Eddy 1 78 65 89 80
Jim 2 89 65 87 76
Jane 3 87 97 75 64
I have tried to use the format()
function but could not make it work with the nested dictionary.
I also tried using pandas
and converting to a pd.DataFrame
, but could not make it work.
question from:
https://stackoverflow.com/questions/65546197/print-nested-dictionary-in-table-format 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…