Repository navigation
Expand file tree
/
Copy pathgradeProblemCountJsonGenerator.py
More file actions
79 lines (61 loc) · 2.32 KB
/
Copy pathgradeProblemCountJsonGenerator.py
File metadata and controls
79 lines (61 loc) · 2.32 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
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
__author__ = 'linglin'
import csv
from os.path import expanduser
from collections import OrderedDict
import json
import re
home = expanduser("~")
grade_id = 9
problem_count_csv = home + '/Downloads/g'+ str(grade_id) +'_problem_count (1).csv'
problem_count_out = home + '/Documents/g'+ str(grade_id) +'_problem_count_out.json'
out_file = home + '/Documents/g' + str(grade_id) + '_problem_count_output.json'
schema_test_session = ['problem_id','skill','exercise']
schema_problem_count = ['skill','democount','assistedexercisecount','numberofProblemstobeafficient']
def generateProblemCountJson():
json_str=""
print "problem_count_csv: ", problem_count_csv
with open(problem_count_csv, "r") as f_obj:
json_data=csv_reader(f_obj)
#target=open(problem_count_out,'w')
#target.write(json_str)
json_str = json.dumps(json_data, indent=2, separators=(',', ': '))
#json_str = json_data
#print json_str
with open(out_file, 'w+') as outfile:
json.dump(json_str, outfile)
def csv_reader(file_obj):
"""
Read a csv file
"""
# with open(problem_count_out, "r") as rfile:
# jolddata = rfile.read()
# jdata = json.JSONDecoder(object_pairs_hook=OrderedDict).decode(jolddata)
# problems = jdata
problems = []
reader = csv.reader(file_obj)
section_regx = re.compile("\d{1,2}\.\d{1,2}")
for row in reader:
if(len(row)>0):
#data=row[0].split('\t')
#data=row.split(',')
rowdata = row
if len(rowdata)>0:
print rowdata[0]," ",rowdata[3]," ",rowdata[4]," ",rowdata[7]
# obj={"problem_id": data[0],"skill":data[1],"exercise":data[2]}
if section_regx.match(rowdata[0]):
row_vals = [rowdata[0],rowdata[3], rowdata[4], rowdata[7]]
skill = OrderedDict()
for idx, item in enumerate(schema_problem_count):
if idx == 0:
dvalue = row_vals[idx]
else:
dvalue = int(row_vals[idx])
skill[item] = dvalue
problems.append(skill)
for p in problems:
print p
print problems
print type(problems)
return problems
if __name__ == '__main__':
generateProblemCountJson()