-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoCSV.py
More file actions
53 lines (43 loc) · 1.37 KB
/
Copy pathtoCSV.py
File metadata and controls
53 lines (43 loc) · 1.37 KB
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
# This code takes the data from the text file and adds it to the csv file.
import re
import csv
number_of_questions = 16
number_of_options = 5
number_of_studs = 10
w, h = number_of_questions*number_of_options + 2, number_of_studs;
Matrix = [["0" for x in range(w)] for y in range(h)]
def initr():
with open("/Users/pavan/Desktop/Research/Masters_Project/SRC/assessments/assess1_LP.txt",'r') as f:
mylist = f.read().splitlines()
return mylist
def scrape(lines):
ID = []
r = 1
for line in lines:
my_list = line.split("&")
for obj in my_list:
keyval = obj.split("=")
if keyval[0] == "ID":
c = 0
elif keyval[0] == "time":
c = 1
else:
c = int(keyval[0]) + 1
if keyval[1] == "on":
val = "1"
else:
val = keyval[1]
Matrix[r][c] = val
Matrix[0][0] = "ID"
Matrix[0][1] = "Time"
r = r + 1
return Matrix,r-1
# WIPES PREVIOUS ENTRIES INTO CSV. SAVE INSTANCE!!
def save_to_csv(db,r):
with open("/Users/pavan/Desktop/Research/Masters_Project/SRC/assessments/assess1_LP.csv", "wb") as f:
writer = csv.writer(f)
writer.writerows(db[0:r+1][:])
if __name__ == '__main__':
lines = initr()
db,r = scrape(lines)
save_to_csv(db,r)