Section 15.11 Write Code Questions
Click on the button below to see the contents of books.xml. It contains xml tags to define the books in a bookstore.
Hint 1.<pre id="books.xml"> <?xml version="1.0" encoding="UTF-8"?> <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="web" cover="paperback"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
Checkpoint 15.11.1.
Fix the errors in the code below so that it reads the data from books.xml and finds all of the book data and prints the title for each book and then finds all the author names for each book and prints each author name.
import xml.etree.ElementTree as ET
fh = open("books.xml")
data = fh.read()
fh.close()
tree = ET.fromstring(data)
book_list = tree.find('book')
for book in book_list:
print(f'Book title: {book.find(title).text}')
author_list = book.find('author')
for author in author_list:
print(f"Author: {author}")
Click on the button below to see the contents of news.xml. It contains xml tags to define news stories.
Hint 2.<pre id="news.xml"> <?xml version="1.0" encoding="UTF-8"?> <nitf> <head> <title>Colombia Earthquake</title> </head> <body> <headline> <hl1>143 Dead in Colombia Earthquake</hl1> </headline> <byline> <bytag>By Jared Kotler, Associated Press Writer</bytag> </byline> <dateline> <location>Bogota, Colombia</location> <date>Monday January 25 1999 7:28 ET</date> </dateline> </body> </nitf>
Checkpoint 15.11.2.
Fix the errors in the code below so that it reads the data from news.xml and prints the headline and date.
import xml.etree.ElementTree as ET
fh = open("news.xml")
data = fh.read()
fh.close()
tree = ET.fromstring(data)
head = tree.find('head')
title = head.find('title')
print(head)
body = find(body)
dateline = body.find('dateline')
date = dateline.find('date')
print(date)
Click on the button below to see the contents of weather.xml. It contains xml tags to define weather observations.
Hint 3.<pre id="weather.xml"> <?xml version="1.0" encoding="UTF-8"?> <current_observation> <location>New York/John F. Kennedy Intl Airport, NY</location> <station_id>KJFK</station_id> <latitude>40.66</latitude> <longitude>-73.78</longitude> <observation_time_rfc822>Mon, 11 Feb 2008 06:51:00 -0500 EST</observation_time_rfc822> <weather>A Few Clouds</weather> <temp_f>11</temp_f> <temp_c>-12</temp_c> <relative_humidity>36</relative_humidity> <wind_dir>West</wind_dir> <wind_degrees>280</wind_degrees> <wind_mph>18.4</wind_mph> <wind_gust_mph>29</wind_gust_mph> <pressure_mb>1023.6</pressure_mb> <pressure_in>30.23</pressure_in> <dewpoint_f>-11</dewpoint_f> <dewpoint_c>-24</dewpoint_c> <windchill_f>-7</windchill_f> <windchill_c>-22</windchill_c> <visibility_mi>10.00</visibility_mi> </current_observation>
Checkpoint 15.11.3.
Finish the code below so that it reads the data from weather.xml into a tree and then prints the location, temp_f, and windchill_f for the current_observation.
import xml.etree.ElementTree as ET
fh = open("weather.xml")
data = fh.read()
fh.close()
tree = ET.fromstring(data)
The file email.json
below contains JSON data for people including their first_name, last_name and email address.
Hint 4.<pre id="email.json."> [{ "id": 1, "first_name": "Jeanette", "last_name": "Penddreth", "email": "jpenddreth0@census.gov", "gender": "Female", "ip_address": "26.58.193.2" }, { "id": 2, "first_name": "Giavani", "last_name": "Frediani", "email": "gfrediani1@senate.gov", "gender": "Male", "ip_address": "229.179.4.212" }, { "id": 3, "first_name": "Noell", "last_name": "Bea", "email": "nbea2@imageshack.us", "gender": "Female", "ip_address": "180.66.162.255" }, { "id": 4, "first_name": "Willard", "last_name": "Valek", "email": "wvalek3@vk.com", "gender": "Male", "ip_address": "67.76.188.26" }]
Checkpoint 15.11.4.
Finish the code below so that it prints the first name, last name and email address for each person in the list of dictionaries returned from json.loads(data).
import json
fh = open("email.json")
data = fh.read()
fh.close()
person_list = json.loads(data)
The file person.json
below contains JSON data for a person in a dictionary including their first name, last name, address, and phone numbers.
Hint 5.<pre id="person.json."> { "firstName": "John", "lastName": "Smith", "isAlive": true, "age": 27, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "phoneNumbers": [ { "type": "home", "number": "212 555-1234" }, { "type": "office", "number": "646 555-4567" }, { "type": "mobile", "number": "123 456-7890" } ], "children": [], "spouse": null }
Checkpoint 15.11.5.
Finish the code below so that it prints the first name, last name, the state the person lives in, and their mobile phone number.
import json