Jed Rembold & Fred Agbo
April 7, 2025
Consider the program on the right hand side and the structure of the
defined data. What will be printed to the terminal?
course1[CS151]["Jake"]: {'A', 'B', 'C'}
['Jake': {'A', 'B', 'C'}]
Jake {'A', 'C', 'B'}
Sally {'A', 'B', 'B'}
data ={
"CS151": {
"Sally": {"A", "B", "B"},
"Jake": {"B", "A", "B", "C"},
"Ben": {"A", "B", "A", "B"}
},
"DATA152": {
"James": ["B", "C", "A"],
"Lily": ["A", "A", "B"]
}
}
course1 = data["CS151"]
course2 = data["DATA152"]
for std, grade in course1.items():
if len(grade) == len(course2["Lily"]):
print(std, grade)
json
json.load(file_handle)
json.dump(data_object, file_handle)
with open() as fhandle:
syntaxTo read a JSON file into a variable
data
:
import json
with open('file.json') as fh:
data = json.load(fh)
To write a variable with complex structure out to a JSON file:
import json
with open('file.json', 'w') as fh:
json.dump(data, fh)
[1, 2, 3,]
is perfectly
fine in Python, but illegal in JSON