-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadAndConvert.py
105 lines (67 loc) · 1.87 KB
/
loadAndConvert.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
import os
import posixpath
import csv
import array as arr
import sys
def main():
path = sys.argv[1]
abspath = os.path.abspath(path)
raw = readFile(abspath)
converted = convertFile(raw)
#print(converted)
newArr = calcFile(converted)
newCsv = createCsv(newArr)
writeFile(newCsv)
def writeFile(tArr):
absWritepath = os.path.abspath(sys.argv[2])
resultFile = open(absWritepath,'wb')
wr = csv.writer(resultFile)
for x in tArr :
print(x)
wr.writerow ([x])
def createCsv(tArr):
header = ["created_at", "views", "addViews", "signatures", "addSignatures"]
csv = []
csv.append("created_at,views,addViews,signatures,addSignatures")
delimiter = ","
for tEle in tArr:
line = ""
for i,key in enumerate(header):
line+=str(tEle[key])
if i!=(len(header)-1):
line+=delimiter
csv.append(line)
#print(line)
return csv
def calcFile(arr):
plain = []
for i,row in enumerate(arr):
nLine = {}
nLine['created_at'] = row['created_at']
nLine['views'] = int(row['views'])
nLine['signatures'] = int(row['signatures'])
if i>0:
nLine['addViews'] = int(row['views']) + plain[i-1]['addViews']
nLine['addSignatures'] = int(row['signatures']) + plain[i-1]['addSignatures']
else:
nLine['addViews'] = int(row['views'])
nLine['addSignatures'] = int(row['signatures'])
plain.append(nLine)
#print(plain)
return plain
def readFile(path):
raw = []
with open(path,'rb') as csvfile:
#reader = csv.reader(csvfile, delimiter=';', quotechar='|')
fieldnames = ['created_at', 'views', 'signatures']
reader = csv.DictReader(csvfile, fieldnames)
for i,row in enumerate(reader):
if i>0:
raw.append(row)
#sys.exit()
return raw
def convertFile(raw):
#for row in raw:
return raw
if __name__ == '__main__':
main()