-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_csv.py
44 lines (34 loc) · 1.22 KB
/
handle_csv.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding: utf-8 -*-
import csv
def read_csv(csvfile):
data = []
reader = csv.DictReader(csvfile)
for row in reader:
data.append(row)
return data
def load_data(popfile, co2file, countryfile):
popdata = {x["Country Code"]: x for x in read_csv(popfile)}
co2data = {x["Country Code"]: x for x in read_csv(co2file)}
countrydata = read_csv(countryfile)
countries = {}
for row in countrydata:
code = row["Country Code"]
# first four columns don't have important data: country code & name, indicator name & code
years = list(popdata[code].keys())[4:] # both sets have same starting year
data = {
int(year): (
float(co2data[code][year]) if co2data[code][year] else None,
int(popdata[code][year]) if popdata[code][year] else None,
)
for year in years
if year
} # each line ends in a comma, so the last field is empty
entry = {
"name": row["TableName"],
"region": row["Region"],
"income": row["IncomeGroup"],
"notes": row["SpecialNotes"],
"data": data,
}
countries[code] = entry
return countries