-
Notifications
You must be signed in to change notification settings - Fork 7
/
run.py
88 lines (82 loc) · 2.21 KB
/
run.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import csv
import zipfile
fp = 'data/FX_csv_2.zip'
outfp = 'data/consolidated.csv'
writer = csv.writer(open(outfp, 'w'))
writer.writerow(['Date', 'Rate', 'Currency', 'Frequency'])
zfo = zipfile.ZipFile(fp)
fo = zfo.open('FX_csv_2/README_SERIES_ID_SORT.txt')
def run_all_files():
start = False
end = False
for line in fo.readlines():
if start:
if line.strip() == '':
end = True
if start and not end:
parts = line.split(';')
id_ = parts[0].strip()
country = parts[1].split('/')[0].strip()
if country == 'U.S.':
country = parts[1].split('/')[1].strip().split(' ')[0].strip()
freq = parts[3].strip()
# we don't want trade-weighted exchange rates
if ('TWEX' in id_ or
country == 'Foreign Exchange Rate: Euro Community (DISCONTINUED SERIES)'
):
continue
try:
extract(id_, country, freq)
except Exception, inst:
print id_, country
raise
if line.startswith('File'):
start = True
iso_codes = {
'Brazil': 'BRL',
'Canada': 'CAD',
'China': 'CNY',
'Denmark': 'DKK',
'Hong Kong': 'HKD',
'India': 'INR',
'Japan': 'JPY',
'South Korea': 'KRW',
'Malaysia': 'MYR',
'Mexico': 'MXN',
'Norway': 'NOK',
'Austria': 'ATS',
'Belgium': 'BEF',
'Finland': 'FIM',
'France': 'FRF',
'Germany': 'DEM',
'Greece': 'GRD',
'Italy': 'ITL',
'Netherlands': 'NLG',
'Portugal': 'PTE',
'Sweden': 'SEK',
'South Africa': 'ZAR',
'Singapore': 'SGD',
'Sri Lanka': 'LKR',
'Spain': 'ESP',
'Switzerland': 'CHF',
'Taiwan': 'TWD',
'Thailand': 'THB',
# Pre 2008 it was 'VEB'
'Venezuela': 'VEF',
'Australia': 'AUD',
# New Zealand
'New': 'NZD',
'U.K': 'GBP',
'Euro': 'EUR',
'Ireland': 'IEP'
}
def extract(id_, country, freq):
fp = 'FX_csv_2/data/%s' % id_
fo = zfo.open(fp)
reader = csv.reader(fo)
reader.next()
for row in reader:
outrow = row + [iso_codes[country], freq]
writer.writerow(outrow)
fo.close()
run_all_files()