-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_can_export.py
More file actions
176 lines (136 loc) · 4.12 KB
/
Copy pathparse_can_export.py
File metadata and controls
176 lines (136 loc) · 4.12 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import sys
import os
os.environ["path"] = os.path.dirname(sys.executable) + ";" + os.environ["path"]
import glob
import operator
import re
import datetime
import dateutil.relativedelta
import win32gui
import win32ui
import win32con
import win32api
import numpy
import json
import csv
import urllib.request
import urllib.error
import scipy.ndimage
import scipy.stats
import multiprocessing
import matplotlib.pyplot as plt
from PIL import Image
from time import strftime
from time import sleep
from operator import itemgetter
###############################################################################
# GLOBAL CONSTANTS
###############################################################################
DATA_FOLDER = "data"
DOC_FOLDER = "documentation"
EXPORT_FILE = os.path.join(DOC_FOLDER, "en_visualize_explore_tree_map_hs92_export_can_all_show_2016.csv")
NAME_FILE = os.path.join(DOC_FOLDER, "products_hs_92.tsv")
PRINT_LEVEL=0
###############################################################################
# UTILITIES
###############################################################################
def myprint(str, level=0):
if (level >= PRINT_LEVEL):
print(str)
def as_float(obj):
for k in obj:
try:
obj[k] = float(obj[k])
except (ValueError, TypeError):
pass
return obj
def as_float_list(obj):
for i in range(len(obj)):
try:
obj[i] = float(obj[i])
except (ValueError, TypeError):
pass
return obj
def individual_to_csv(data):
years = sorted(list(data.keys()))
header = [["year"] + [x for x in years]]
year0 = years[0]
results = []
for measure in data[year0]:
results.append([measure])
for year in years:
for i in range(len(results)):
measure = results[i][0]
if measure in data[year]:
results[i].append(data[year][measure])
else:
results[i].append("N/A")
return header + results
###############################################################################
# PROCESSES
###############################################################################
def read_file_as_csv(filename, separator):
csv_array = []
with open(filename, 'r') as names:
for line in names:
line = line.replace("\"", "")
line = [n for n in line.split(separator)]
csv_array.append(line)
return csv_array
def map_id_to_name(results):
data = read_file_as_csv(NAME_FILE, ' ')
data_as_dict = {}
for row in data[1:]: # skip header
if row[1] in data_as_dict:
myprint("Warning, ID " + str(row[1]) + " already exists with mapping : " + str(data_as_dict[row[1]]) + ", will be replaced by : " + str(row[2]),5)
data_as_dict[row[1]] = row[2]
results["name_map"] = data_as_dict
def read_export(results):
data2 = read_file_as_csv(EXPORT_FILE, ',')
results["exports"] = data2
def combine_results(results):
final_export = []
export_data = results["exports"]
name_data = results["name_map"]
for row in export_data[1:]: # skip header
id = row[3]
val = row[4]
if id not in name_data:
myprint("ERROR: Could not find id " + id + " in name_data",5)
name = name_data[id]
final_export.append({"name":name, "val":val})
results["combined"] = final_export
myprint(str(results["combined"]))
###############################################################################
# MAIN
###############################################################################
def do_actions(actions, params):
results = {}
if "parse_names" in actions:
map_id_to_name(results)
if "parse_export" in actions:
read_export(results)
if "combine" in actions:
combine_results(results)
if __name__ == '__main__':
actions = [
"parse_names",
"parse_export",
"combine",
"nothing" # just so I don't need to play with the last ,
]
params = {
}
do_actions(actions, params)
'''
globpat = os.path.join(DATA_FOLDER, "financials", "**", "*.json")
financials = glob.glob(globpat)
financials = [os.path.basename(os.path.dirname(n)) for n in financials]
myprint(financials)
with open(STOCK_LIST, 'r') as jsonfile:
symbols = json.load(jsonfile)
symbols = list(symbols.keys())
symbols = [symbol.replace("-", ".") for symbol in symbols]
myprint(str(set(symbols) - set(financials)))
sys.exit(0)
'''