-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvhandler.py
More file actions
126 lines (94 loc) · 2.88 KB
/
Copy pathcsvhandler.py
File metadata and controls
126 lines (94 loc) · 2.88 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
# csvhandler - david.barkhuizen@gmail.com - 2010-09-06 -> ?-?-?
import csv
import datetime
from decimal import *
def load_csv_list_from_file(file_path):
f = open(file_path, 'r')
line = f.readline()
f.close()
splut = line.split(',')
tokens = []
for token in splut:
cleaned = token.strip()
if len(cleaned) > 0:
tokens.append(cleaned)
return tokens
def parse_string_to_date(date_str):
'''parse date string of format yyyy-mm-dd to datetime.date'''
split_chars = ['-', '\\', '/', '.']
split_char = '-'
for char in split_chars:
if (date_str.find(char) != -1):
split_char = char
break
try:
tokens = date_str.split(split_char)
y = int(tokens[0].lstrip('0'))
m = int(tokens[1].lstrip('0'))
d = int(tokens[2].lstrip('0'))
return datetime.date(y, m, d)
except Exception, e:
return None
def row_to_dict(row):
'''return dict with keys [date, open, high, low, close, volume, adj_close]'''
date = parse_string_to_date(row[0])
if (date == None):
return None
open = Decimal(row[1])
high = Decimal(row[2])
low = Decimal(row[3])
close = Decimal(row[4])
try:
volume = Decimal(row[5])
except:
volume = 0
try:
adj_close = Decimal(row[6])
except:
adj_close = 0
return {'date' : date, 'open' : open, 'high' : high, 'low' : low, 'close' : close, 'adj_close' : adj_close, 'volume' : volume }
def load_csv_data_rows(path_to_csv):
'''load specified csv file, return list of rows (including header row, if any)'''
csv_file = open(path_to_csv, 'r')
reader = csv.reader(csv_file)
csv_file = open(path_to_csv, 'r')
data_rows = []
line_count = 0
exit_loop = False;
while exit_loop == False:
try:
row = reader.next()
data_rows.append(row)
line_count += 1
except StopIteration:
exit_loop = True
csv_file.close()
return data_rows
def rows_to_dicts(rows):
dicts = []
for r in rows:
#try:
dict = row_to_dict(r)
if (dict != None):
dicts.append(dict)
#except Exception, e:
# print(e)
return dicts
def parse_rows_to_distinct_lists(data_rows, clip_first_line=True):
'''
return (date, open, high, low, close, adj_close, volume) from list of data_rows
'''
date, open, high, low, close, adj_close, volume = [], [], [], [], [], [], []
start_idx = 0
if clip_first_line == True:
start_idx = 1
for i in range(start_idx, len(data_rows)):
row_data = row_to_dict(data_rows[i])
date.append(row_data['date'])
open.append(row_data['open'])
high.append(row_data['high'])
low.append(row_data['low'])
close.append(row_data['close'])
adj_close.append(row_data['adj_close'])
volume.append(row_data['volume'])
return date, open, high, low, close, adj_close, volume