-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_formatter.py
62 lines (59 loc) · 2.5 KB
/
file_formatter.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
import os
stocks = open('stocks_copy.txt')
stock_data = stocks.read().split('\n')
current_dir = os.path.dirname(__file__)
print current_dir
def get_rid_of_commas(data):
other_temp = ''
for i in xrange(0, len(data) - 1):
other_temp += data[i]
data[0] = other_temp
data = [data[0], data[-1]]
return data
with open('New_Data/Top_Data.csv', "w") as outfile:
outfile.write("Year,Top_Search,Score,Ticker")
outfile.write('\n')
for stock in stock_data:
for year in xrange(2004, 2014):
get = "%s_%s_top.csv" % (stock, year)
folder = "Stock_Data/%s/top/" % (stock)
try:
with open(folder + get, 'rb') as infile:
year_data = infile.read().split('\n')
for line in year_data:
if line != '':
outfile.write("%s," % (year))
data = line.split(',')
if len(data) > 2:
data = get_rid_of_commas(data)
outfile.write(','.join(data))
outfile.write(",%s" % (stock))
outfile.write('\n')
except IOError:
print "%s not found" % (get)
with open('New_Data/Rising_Data.csv', "w") as outfile:
outfile.write("Year,Rising_Search,Score,Ticker")
outfile.write('\n')
for stock in stock_data:
for year in xrange(2004, 2014):
get = "%s_%s_rising.csv" % (stock, year)
folder = "Stock_Data/%s/rising/" % (stock)
try:
with open(folder + get, 'rb') as infile:
year_data = infile.read().split('\n')
for line in year_data:
if line != '':
outfile.write("%s," % (year))
data = line.split(',')
if len(data) > 2:
if data[-2][0] == '+':
temp = data[-2] + data[-1]
data[-2] = temp
del data[-1]
if len(data) > 2:
data = get_rid_of_commas(data)
outfile.write(','.join(data))
outfile.write(",%s" % (stock))
outfile.write('\n')
except IOError:
print "%s not found" % (get)