ServerPortfolio  2.0
Python parsers and server
 All Classes Namespaces Files Functions Variables Properties Pages
Script_FormatCSV.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 import sys
4 import string
5 
6 import datetime
7 
8 # correct header, better to do
9 # correct date
10 # to do more use of wrapper
11 
12 def printUsage():
13  print '''
14  ===================================================================
15  USAGE
16  ./Script_FormatCSV input_file FORMAT_input FORMAT_output > output_file
17 
18  FORMAT: CSV_Nyse /implemented
19  CSV_Yahoo /implemented
20  MyCSV /by default, not implemented
21  MydCSV
22  dCSV_Bourso /not implemented, no use just modify output in Server
23  / to modify output of Server, temporary
24 
25  ===================================================================
26  '''
27 
28 class CSV:
29 
30  def __init__(self,format_in,format_out,file):
31 
32  self.ListCSV=[]
33 
34  self.format_in=format_in
35  self.format_out=format_out
36  self.file=file
37 
38 
39  def write(self):
40 
41  #print "# 5"
42  # #0.1 DAY 2012-10-10 2012-10-22
43  # To use wrapper C++ for header ?
44  print "#0.1 DAY "
45 
46  for i in range(len(self.ListCSV)):
47 
48  dictCSV = self.ListCSV[i]
49  #print "dictCSV ",dictCSV
50  if self.format_out == "MyCSV":
51  print "%s %s,%.3f,%.3f,%.3f,%.3f,%i" %(dictCSV['date'],dictCSV['time'],dictCSV['open'],dictCSV['high'],dictCSV['low'],dictCSV['close'],dictCSV['volume'])
52 
53  elif self.format_out == "MydCSV":
54  print "%s %s,%.3f,%i" %(dictCSV['date'],dictCSV['time'],dictCSV['value'],dictCSV['volume'])
55 
56 
57  def read(self):
58 
59 
60  status_data = False
61 
62  for line in self.file.xreadlines():
63 
64  dictCSV={}
65 
66  ##Read comment
67  if status_data == False:
68 
69 
70  if self.format_in == "CSV_Nyse" :
71 
72  ret = string.find(line,"Date;opening;High;Low;closing;Volume")
73  #print "ret ", ret
74  #if ret ==0 :
75  # status_data = True
76 
77  if self.format_in == "CSV_Yahoo":
78  ret = string.find(line,"Date,Open,High,Low,Close,Volume,Adj Close")
79  #print "ret ", ret
80 
81  if self.format_in == "dCSV_Bourso":
82  ret = string.find(line,"#")
83  #print ret
84 
85 
86  if ret == 0 :
87  status_data = True
88 
89  #status_data is True
90  else:
91 
92  if self.format_in == "CSV_Nyse" :
93 
94  tabline=line.split(";")
95 
96  #extract datetime
97  ## strptime is a static method of the datetime class
98  date_tmp=datetime.datetime.strptime( tabline[0], "%d/%m/%y" )
99 
100  dictCSV['date'] = date_tmp.strftime("%d-%m-%Y")
101  dictCSV['time']="00:00:00"
102  dictCSV['open']=float(tabline[1])
103  dictCSV['high']=float(tabline[2])
104  dictCSV['low']=float(tabline[3])
105  dictCSV['close']=float(tabline[4])
106  dictCSV['volume']=int(tabline[5])
107 
108  if self.format_in == "CSV_Yahoo" :
109 
110  tabline=line.split(",")
111 
112  #extract datetime
113  ## strptime is a static method of the datetime class
114  #print "tabline[0] ", tabline[0]
115  date_tmp=datetime.datetime.strptime( tabline[0], "%Y-%m-%d" )
116 
117  # original
118  #dictCSV['date'] = date_tmp.strftime("%d-%m-%Y")
119  # more recent, do not change format, could copy or test correct parsing
120  dictCSV['date']=date_tmp.strftime("%Y-%m-%d")
121  dictCSV['time']="00:00:00"
122  dictCSV['open']=float(tabline[1])
123  dictCSV['high']=float(tabline[2])
124  dictCSV['low']=float(tabline[3])
125  dictCSV['close']=float(tabline[4])
126  dictCSV['volume']=int(tabline[5])
127  # adjusted close would be 6
128 
129  if self.format_in == "dCSV_Bourso" :
130  tabline=line.split(",")
131  #print "tabline ",tabline
132  #date_tmp=datetime.datetime.strptime( tabline[0], "%d/%m/%y %H:%M:%S" )
133  date_tmp=datetime.datetime.strptime( tabline[0], "%d-%m-%y %H:%M:%S" )
134  dictCSV['date'] = date_tmp.strftime("%Y-%m-%d")
135  #dictCSV['time']="00:00:00"
136  dictCSV['time'] = date_tmp.strftime("%H:%M:%S")
137  dictCSV['value'] = float(tabline[1])
138  dictCSV['volume'] = int(round(float(tabline[2]),0))
139 
140 
141  self.ListCSV.append(dictCSV)
142  #print self.ListCSV
143 
144  ##end reading
145  self.file.close()
146 
147  ##want reverse of chronologic order
148  if self.format_in == "CSV_Nyse":
149  self.ListCSV.reverse()
150 
151 ###end class CSV
152 
153 
154 
155 ####### main
156 ListFormat= [ "CSV_Nyse", "CSV_Yahoo", "dCSV_Bourso", "MyCSV", "MydCSV" ]
157 
158 try:
159  Infile = open(sys.argv[1],'r')
160  Format_in = sys.argv[2]
161  Format_out = sys.argv[3]
162 except IndexError:
163  printUsage()
164  sys.exit(1)
165 
166 #print "infile ",Infile
167 #print "Format_in ", Format_in
168 #print "Format_out ", Format_out
169 
170 if ( ( Format_in not in ListFormat ) or ( Format_out not in ListFormat) ):
171  print "Error Format"
172  printUsage()
173  sys.exit(1)
174 
175 CSV = CSV( Format_in, Format_out, Infile )
176 CSV.read()
177 
178 CSV.write()
format_in
Read comment.