-
Notifications
You must be signed in to change notification settings - Fork 6
/
FormatData.py
111 lines (87 loc) · 2.94 KB
/
FormatData.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
import sys
import os
import csv
stocks = []
#Initializes Stock list
with open("Stocks.txt", "r") as file:
for line in file:
st = line.split(",")
for stock in st:
stocks.append(stock.rstrip())
filename = 'StockDataCurrent.csv'
print(stocks)
#Removes current file
try:
os.remove(filename)
except OSError:
pass
first = True
#Formats data in the files correctly for the Portfolio Manager
for stock in stocks:
try:
csvfile = open('StockData/%s.csv' % stock, 'r')
except:
print('Skipped: ' + stock)
continue
reader = csv.DictReader(csvfile)
all_lines = list(reader)
with open(filename, 'a') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
if (first):
first = False
dates = []
dates.append('Name')
dates.append('Type')
#Write the headers
for i in range(len(all_lines)):
Day = int(i)
x = all_lines[Day]['date'].split("-")
date = x[1] + "/" + x[2] + "/" + x[0]
dates.append(date)
filewriter.writerow(dates)
#Write the ClosePrice
closePrice = []
closePrice.append(stock)
closePrice.append('open')
for i in range(len(all_lines)):
Day = int(i)
closePrice.append(all_lines[Day]['1. open'])
filewriter.writerow(closePrice)
#Write the HighPrice
openPrice = []
openPrice.append(stock)
openPrice.append('high')
for i in range(len(all_lines)):
Day = int(i)
openPrice.append(all_lines[Day]['2. high'])
filewriter.writerow(openPrice)
#Write the LowPrice
highPrice = []
highPrice.append(stock)
highPrice.append('low')
for i in range(len(all_lines)):
Day = int(i)
highPrice.append(all_lines[Day]['3. low'])
filewriter.writerow(highPrice)
#Write the LowPrice
lowPrice = []
lowPrice.append(stock)
lowPrice.append('close')
for i in range(len(all_lines)):
Day = int(i)
lowPrice.append(all_lines[Day]['4. close'])
filewriter.writerow(lowPrice)
#Write the LowPrice
volume = []
volume.append(stock)
volume.append('Volume')
for i in range(len(all_lines)):
Day = int(i)
volume.append(all_lines[Day]['6. volume'])
filewriter.writerow(volume)
#Eliminates extra space at end of file
with open(filename, 'rb+') as f:
f.seek(0,2) # end of file
size=f.tell() # the size...
f.truncate(size-1)