-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata.py
More file actions
28 lines (21 loc) · 843 Bytes
/
data.py
File metadata and controls
28 lines (21 loc) · 843 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from typing import List, Tuple
from xml.etree import ElementTree
def load_wb_xml(path: str, region_key: str) -> List[Tuple[int, float]]:
"""
Parse time series stored in world bank format
:param path: File containing data
:param region_key: Queried region code. World bank does use 3-letter
codes for each country/region
:return: List of paired (year, value associated with given year)
"""
tree = ElementTree.parse(path)
records = tree.findall(
".//record/*[@name='Country or Area'][@key='%s']/.." % region_key
)
data = []
for record in records:
year = record.findtext("field[@name='Year']")
value = record.findtext("field[@name='Value']")
if year is not None and value:
data.append((int(year), float(value)))
return data