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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Dataset conversion script
Author: Gertjan van den Burg
"""
import json
import argparse
import clevercsv
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("input_file", help="File to convert")
parser.add_argument("output_file", help="File to write to")
return parser.parse_args()
def main():
args = parse_args()
with open(args.input_file, "r", newline="", encoding="UTF-8-SIG") as fp:
reader = clevercsv.reader(
fp, delimiter=",", quotechar='"', escapechar=""
)
rows = list(reader)
rows = rows[4:]
header = rows.pop(0)
as_dicts = []
for row in rows:
as_dicts.append({h: v for h, v in zip(header, row)})
iran = next(
(d for d in as_dicts if d["Country Name"] == "Iran, Islamic Rep."),
None,
)
tuples = []
for key in iran:
try:
ikey = int(key)
except ValueError:
continue
if not iran[key]:
continue
tuples.append((ikey, float(iran[key])))
name = "gdp_iran"
longname = "GDP Iran"
time = [str(t[0]) for t in tuples]
time_fmt = "%Y"
series = [
{
"label": "GDP (constant LCU)",
"type": "float",
"raw": [t[1] for t in tuples],
}
]
data = {
"name": name,
"longname": longname,
"n_obs": len(time),
"n_dim": len(series),
"time": {
"type": "string",
"format": time_fmt,
"index": list(range(len(time))),
"raw": time,
},
"series": series,
}
with open(args.output_file, "w") as fp:
json.dump(data, fp, indent="\t")
if __name__ == "__main__":
main()
|