-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.py
63 lines (46 loc) · 1.99 KB
/
process.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import csv
import json
def convert():
trade_by_country = {}
with open('data.csv') as data:
reader = csv.reader(data)
headers = reader.next()
for row in reader:
direction = row[0].lower()
year = row[1].replace('FY', '')
country = row[2]
commodity = row[3]
try:
aud_thousands = float(row[4])
except ValueError:
aud_thousands = 0
if not country in trade_by_country.keys():
trade_by_country[country] = {
'balance' : {},
'exports' : {},
'imports' : {}
}
if year not in trade_by_country[country][direction].keys():
trade_by_country[country][direction][year] = 0
if year not in trade_by_country[country]['balance'].keys():
trade_by_country[country]['balance'][year] = 0
trade_by_country[country][direction][year] += aud_thousands
if direction == 'exports':
trade_by_country[country]['balance'][year] += aud_thousands
else:
trade_by_country[country]['balance'][year] -= aud_thousands
trade_by_country['ALL'] = None
for country, values in trade_by_country.items():
if country == 'ALL':
continue
if trade_by_country['ALL'] is None:
trade_by_country['ALL'] = values
else:
for direction, years in values.items():
for year, value in years.items():
trade_by_country['ALL'][direction][year] += value
print trade_by_country['ALL']
with open('trade-by-country.json', 'w') as tbc:
tbc.write('var TRADE_BY_COUNTRY = {0};'.format(json.dumps(trade_by_country)))
if __name__ == '__main__':
convert()