From c11fc9effebc74dd2de95481f387dc4ad08eeb75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kn=C3=BCttel?= Date: Wed, 12 Dec 2018 15:17:33 +0100 Subject: [PATCH] added Ex 27 --- ex_27.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 ex_27.py diff --git a/ex_27.py b/ex_27.py new file mode 100644 index 0000000..33193eb --- /dev/null +++ b/ex_27.py @@ -0,0 +1,33 @@ +from defusedxml import ElementTree +from collections import deque +import matplotlib.pyplot as plt + +data = {"Germany": deque() + , "France": deque() + , "Italy": deque() + , "United States": deque() +} + +with open("data/API_NY.GDP.MKTP.KN_DS2_en_xml_v2_10230884.xml") as fin: + tree = ElementTree.parse(fin) + for record in tree.getroot().find("data").findall("record"): + this_data = {field.get("name"): field.text for field in record.findall("field")} + if(this_data["Country or Area"] in data): + if(this_data["Value"] != None): + data[this_data["Country or Area"]].append((this_data["Year"], this_data["Value"])) + + + +class Data(object): + def __init__(self, raw_data): + self.x = [int(k) for k, v in raw_data] + self.y = [float(v) for k, v in raw_data] + +data = {k: Data(v) for k,v in data.items()} + +plots = [plt.plot(v.x, v.y, label=k)[0] for k, v in data.items()] + +plt.legend(handles = plots) + +plt.show() +