1. Recap and Exercise

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)

In [1]:
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'])
olympic
2018
In [2]:
#print out each sport
for sport in sports:
    print(sport.tag)
curling
snowboarding
In [3]:
#print out the athletes for each sport
for sport in sports:
    print(sport.tag)
    for athlete in sport:
        print(athlete.text)
curling

  			Bill
		  

  			Shelly
		  
snowboarding

  			Shaun
		  
In [5]:
import json
with open('class10_files/sports.json') as f: 
    s = f.read()
d= json.loads(s)
olympics = d['olympics']
print(olympics)
{'year': '2018', 'sports': {'curling': ['Bill', 'Shelly'], 'snowboarding': {'Shawn': ''}}}
In [6]:
for attributes in olympics: 
    print(attributes)
year
sports
In [6]:
print(olympics['year'])
2018
In [7]:
print(olympics['sports'])    
{'curling': ['Bill', 'Shelly'], 'snowboarding': {'Shawn': ''}}
In [8]:
for sport in olympics['sports']: 
    print(sport)
curling
snowboarding
In [9]:
for sport in olympics['sports']: 
    print(sport)
    for athlete in olympics['sports'][sport]: 
        print('  - '+athlete)        
curling
  - Bill
  - Shelly
snowboarding
  - Shawn

2. Colormaps

Click here for a notebook on colormaps.