Skip to content

Commit 124d7b0

Browse files
authored
Add files via upload
1 parent aeac735 commit 124d7b0

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

CSVAttribution.py3

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import xlrd
2+
import csv
3+
from datetime import datetime
4+
import sys
5+
import os.path
6+
7+
DAILY_EXCEL_FILE_PATH = 'Python Attribution File.xlsx'
8+
MASTER_FILE_PATH = 'Master Attribution CSV.csv'
9+
10+
11+
def convert_path_to_csv(path):
12+
return '.'.join(path.split('.')[:-1]) + '.csv'
13+
14+
def excel_to_csv(path):
15+
wb = xlrd.open_workbook(path)
16+
sh = wb.sheet_by_index(0)
17+
csv_path = convert_path_to_csv(path)
18+
csv_file = open(csv_path, 'w')
19+
wr = csv.writer(csv_file)
20+
date_tuple = xlrd.xldate_as_tuple(sh.row_values(0)[-1], wb.datemode)
21+
date = datetime(*date_tuple).strftime('%m/%d/%Y')
22+
date_fields = [date for i in range(sh.nrows-1)]
23+
date_fields = ['Date'] + date_fields
24+
for rownum in range(sh.nrows):
25+
if rownum == 0:
26+
wr.writerow([date_fields[rownum]] + sh.row_values(rownum)[:-1] + ['Value'])
27+
else:
28+
wr.writerow([date_fields[rownum]] + sh.row_values(rownum))
29+
csv_file.close()
30+
31+
32+
def add_to_master(master_path, csv_path):
33+
master_lines = [line.strip() for line in open(master_path) if line.strip()]
34+
csv_lines = [line.strip() for line in open(csv_path) if line.strip()]
35+
if master_lines:
36+
csv_lines = csv_lines[1:]
37+
master_lines += csv_lines
38+
with open(master_path, 'w') as out:
39+
out.write('\n'.join(master_lines))
40+
41+
if len(sys.argv) == 3:
42+
if not os.path.isfile(sys.argv[1]):
43+
print ("Error: File '" + sys.argv[1] + "' does not exist")
44+
else:
45+
INPUT_FILE = sys.argv[1]
46+
OUTPUT_FILE = sys.argv[2]
47+
print ("Input file = " + INPUT_FILE)
48+
print ("Output file = " + OUTPUT_FILE)
49+
excel_to_csv(INPUT_FILE)
50+
add_to_master(OUTPUT_FILE, convert_path_to_csv(INPUT_FILE))
51+
else:
52+
print ("Error: " + sys.argv[0] + " must have 2 file arguments")

0 commit comments

Comments
 (0)