import json
rows = [
{"name": "Rachel", "age": 34},
{"name": "Monica", "age": 34},
{"name": "Phoebe", "age": 37}
]
with open('testwrite.json', 'w') as f:
json.dump(rows, f)
This snippet reads test.json
and loads the contents as a dict into the variable data
.
import json
with open('friends.json', 'r') as f:
data = json.load(f)
print(data)
In a new file called cookveggies.py
-
Read
output/vegetables.csv
(see previous section) into a variable calledvegetables
. -
Print the variable
vegetables
. -
Write
vegetables
as a JSON file calledoutput/vegetables.json
. It should look like this:[ { "name": "eggplant", "color": "purple" }, { "name": "tomato", "color": "red" }, { "name": "corn", "color": "yellow" }, { "name": "okra", "color": "green" }, { "name": "arugula", "color": "green" }, { "name": "broccoli", "color": "green" } ]
Download superheroes.json
wget https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json
then write a python program that
- Reads
superheroes.json
(in this folder) - Creates an empty array called
powers
- Loop thorough the members of the squad, and append the powers of each to the
powers
array. - Prints those powers to the terminal
Bonus: make the list of powers unique and print it again
hint for bonus: To get the unique elements in a list use the set
method. For example, try running list(set([1, 1, 2, 3]))
in your python console. Alternatively you can use an if statement to only add the powers to the list if they are not already in there.
Lets Read superheroes.json
(in this folder) and output a flat CSV of members. The columns should be: name, age, secretIdentity, powers, squadName, homeTown, formed, secretBase, active
. Any column that is top level, such as squadName
should just be repeated for every row.
Here is an example set of steps:
- Read
superheroes.json
- Write a header to the CSV file
- Loop over the members, and for each member write a row to the csv file
The output should look like this:
name | age | secretIdentity | powers | squadName | homeTown | formed | secretBase | active |
---|---|---|---|---|---|---|---|---|
Molecule Man | 29 | Dan Jukes | ['Radiation resistance', 'Turning tiny', 'Radiation blast'] | Super hero squad | Metro City | 2,016 | Super tower | True |
Madame Uppercut | 39 | Jane Wilson | ['Million tonne punch', 'Damage resistance', 'Superhuman reflexes'] | Super hero squad | Metro City | 2,016 | Super tower | True |
Eternal Flame | 1,000,000 | Unknown | ['Immortality', 'Heat Immunity', 'Inferno', 'Teleportation', 'Interdimensional travel'] | Super hero squad | Metro City | 2,016 | Super tower | True |
Bonus
Write one row per power rather than one row per member.