Last class (click here) you began writing a .xml datafile with your own structure.
Finish this exercise by creating your dataset and opening/reading it using lxml.etree.
Below is my example dataset for olympic sports and athletes.
My files are available here (xml) and here (json)
from lxml import etree
#open svg and read data an xml
with open('class10_files/sports.xml') as f:
sports = etree.fromstring( f.read() )
#attributes of this sports dataset
print(sports.attrib['type'])
print(sports.attrib['year'])
#print out each sport
for sport in sports:
print(sport.tag)
#print out the athletes for each sport
for sport in sports:
print(sport.tag)
for athlete in sport:
print(athlete.text)
import json
with open('class10_files/sports.json') as f:
s = f.read()
d= json.loads(s)
olympics = d['olympics']
print(olympics)
for attributes in olympics:
print(attributes)
print(olympics['year'])
print(olympics['sports'])
for sport in olympics['sports']:
print(sport)
for sport in olympics['sports']:
print(sport)
for athlete in olympics['sports'][sport]:
print(' - '+athlete)