-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcsvloader.py
179 lines (147 loc) · 5.29 KB
/
csvloader.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
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
177
178
179
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
A tool load stock history data from csv file
Author: TerryH, email: terryh.tp at gmail.com,
License: BSD
"""
import datetime
import sys
import getopt
import string
import os
#import mmap
def csvtolist(filename="",backbars=300,maxbars=False,start="",end=""):
""" csvtolist(filename="",backbars=300,maxbars=False,start="",end="")
A tool load stock history data from csv file
filename
the filename you want to process
backbars
how many back bars you take to caculate
you also can test the result from command line
csvtolist.py --backbars=number csvfile.csv
-h --help read the help
-b --backbars the number back bars you want to caculate
read thhe csv file last few lines to process look back data
the format we prefer
YYYY/MM/DD,HH:MM,OPEN,HIGH,LOW,CLOSE,VOLUMN
YYYY/MM/DD,OPEN,HIGH,LOW,CLOSE,VOLUMN
or
YYYY-MM-DD,OPEN,HIGH,LOW,CLOSE,VOLUMN
"""
dl = []
if filename and os.path.isfile(filename):
fp = open(filename,"rb")
#fsize = os.path.getsize(filename)
content = fp.read()
lines = []
si = -1 # start point index
ei = -1
date_split = "?"
dt_samefield = False # datetime
# identify date text
if content.find('/') >= 4 : # AC year 2011/01/01
date_split="/"
elif content.find('-') >= 4:
date_split="-"
# check for start or end
if start:
si = content.find(start)
if end:
ei = content.rfind(end) # look up from end of file
# prepare the return list
if si >0 and ei>0:
lines = content[si:ei].splitlines() # this will strip \r or \n
elif si>0:
lines = content[si:].splitlines()
elif ei>0:
lines = content[:ei].splitlines()
else:
lines = content.splitlines()[-backbars:]
# empty file
if not lines: return dl
# check datetime combination in last line
lastline = lines[-1]
ll= lastline.split(',')
if ll[0].split(" ") > 1 and len(ll) == 6:
# datetime field YYYY/MM/DD HH:MM,OPEN,HIGH,LOW,CLOSE,VOLUMN
dt_samefield = True
for line in lines:
line = line.strip()
if line.startswith(","):
line=line[1:]
if line.endswith(","):
line=line[:-1]
ll = line.split(",")
ll = map(string.strip,ll)
# blanket first value
date = []
o = 0
h = 0
l = 0
c = 0
v = 0
hh = 0
mm = 0
ss = 0
# in case we have date time combine field
datel = ll[0].split(" ")[0]
date = datel.split(date_split)[:3]
date = map(int,date)
if dt_samefield:
# have no HH:MM:SS
#o,h,l,c,v = map(decimal.Decimal,ll[1:])
o,h,l,c,v = map(float,ll[1:])
# may date time at first field
if ll[0].find(":")>2:
# find a mark like time
dtl = ll[0]
m1 = dtl.find(":")
hh = int(dtl[m1-2:m1])
mm = int(dtl[m1+1:m1+3])
ss = 0
elif len(ll)>6:
# having time
t = ll[1].split(':')
t = map(int,t)
hh = t[0]
mm = t[1]
if len(t)==3:
ss = t[2]
else:
ss = 0
#o,h,l,c,v = map(decimal.Decimal,ll[2:7])
o,h,l,c,v = map(float,ll[2:7])
if hh:
dt = datetime.datetime(date[0],date[1],date[2],hh,mm,ss)
else:
dt = datetime.datetime(date[0],date[1],date[2])
dl.append([dt,o,h,l,c,v])
return dl
if __name__ == '__main__':
opts, args = getopt.getopt(sys.argv[1:], "ahb:", ["all","help", "backbars=","start=","end="])
if len(args) > 0:
start=0
end=0
for o, a in opts:
if o in ("-h", "--help"):
print csvtolist.__doc__
sys.exit()
elif o in ("-b", "--backbars"):
import pprint
#pprint.pprint(csvtolist(args[0],int(a)))
#print csvtolist(args[0],int(a))
import timeit
tt = timeit.Timer("csvtolist('%s',%d)"%(args[0],int(a)),"from __main__ import csvtolist")
print "Run csvtolist once take ",tt.timeit(500)/500
elif o in ("-a", "--all"):
print csvtolist(args[0],maxbars=True)
elif o in ("--start"):
start = a
elif o in ("--end"):
end = a
if start or end:
print csvtolist(args[0],start=start,end=end)
else:
print csvtolist.__doc__
sys.exit()