webworktod2l.py

This Python script takes a WebWork score file and produces a CSV file which can be imported into D2L. You will need to modify several variables which appear at the top.
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
# This Python script takes a WebWork score file and produces a CSV file which can be imported into D2L.
# Filename: webworktod2l.py
# Author: Edward D. Kim, UW-La Crosse

# A full path name to the WebWork score file
webworkfilename = 'C:/Users/ekim/Downloads/MTH207_Kim_totals.csv' # Forward slashes because of \U

# A full path name to the file to create
outputfilename = 'C:/Users/ekim/Downloads/D2L-Upload.csv'

# If this string is non-empty, filter the homework sets to keep ONLY those which start with the specified prefix string.
setnameprefix = 'spr2014-'

# If 'yes', the names of the assignments for D2L will have the prefix (set above) removed
removesetnameprefix = 'yes'

# If this string is non-empty, only include students whose recitation in WebWork is the specified string.
recitationfilter = 'spr2014'

# If this string is non-empty, only include students whose section in WebWork is the specified string.
sectionfilter = ''

# Delete the WebWork file once this script is complete? Use string 'yes' for yes, and anything else for no.
deletewebworkfileoncomplete = 'yes'

######################################
######################################
######################################
######################################

import csv
import os
 
with open(webworkfilename, 'rt') as csvfile:
	csvreaderobject = csv.reader(csvfile, delimiter=',')
	# No use of quotechar='' needed

	outputfile = open(outputfilename, 'w')

	column_to_setname_dictionary = {}
	passed_student_id = 0
	
	for row in csvreaderobject:
		if (row[0].strip() == "SET NAME"):
			# This is the line with set names
			#print(row)
			for column in range(len(row)):
				cell = row[column]
				if (cell.find(setnameprefix)!=-1):
					if (removesetnameprefix=='yes'):
						d2lsetname_pre = cell[len(setnameprefix):]
					else:
						d2lsetname_pre = cell
					d2lsetname = d2lsetname_pre.strip()
					column_to_setname_dictionary[column]=d2lsetname
			lineout = ''
			lineout = 'Username,'
			for column in column_to_setname_dictionary:
				lineout = lineout + column_to_setname_dictionary[column] + ' Points Grade,'
			lineout = lineout + 'End-of-Line Indicator'
			print(lineout)
			outputfile.write(lineout+'\n')
		else: # Consider a general row
			if passed_student_id:
				# We have passed all the header rows
				section = row[4].strip()
				recitation = row[5].strip()
				if ((sectionfilter=='') or (sectionfilter==section)) and section!='0' and ((recitationfilter=='') or (recitationfilter==recitation)):

					# The section and recitation filters passed
					username = row[1].strip()

					lineout = ''
					lineout = username + ','
					for column in column_to_setname_dictionary:
						score = row[column].strip()
						lineout = lineout + score + ','
					lineout = lineout + '#'
					print(lineout)
					outputfile.write(lineout+'\n')

			else:
				if (row[0].strip() == "STUDENT ID"):
					passed_student_id = 1
					
if (deletewebworkfileoncomplete == 'yes'):
	if os.path.isfile(webworkfilename):
        	os.remove(webworkfilename)

Powered by Jekyll, theme by Scott Emmons under Creative Commons Attribution. All of the code for this website can be viewed at GitHub.