Skip to content

Instantly share code, notes, and snippets.

@renegarcia
Created February 11, 2022 15:29
Show Gist options
  • Save renegarcia/04d21352035f07c66d857c87ea68e678 to your computer and use it in GitHub Desktop.
Save renegarcia/04d21352035f07c66d857c87ea68e678 to your computer and use it in GitHub Desktop.
This scripts takes a moodle students csv file and returns a webworks (csv) classlist file. Te program WONT ask to overwrite any of the files, be careful.
# USSAGE
# python make_webworks_csv.py <input.csv> <output.lst>
from collections import namedtuple
import csv
import codecs
Student = namedtuple('Student', 'Name Surname Email')
WebworkStudent = namedtuple('WebworkStudent',
'student_id first_name last_name status comment section recitation email_address user_id')
def find_student_id(student):
email = student.Email
stop = email.find('@')
return email[1:stop]
def process_student(student):
name = student.Name.split()[0]
name = name.lower()
surname = student.Surname.split()[0]
surname = surname.lower()
student_id = find_student_id(student)
first_name = student.Name.title()
last_name = student.Surname.title()
status = 'enrolled'
comment = ''
section = ''
recitation = ''
email_address = student.Email.lower()
user_id = f'{name}.{surname}'
return WebworkStudent(student_id=student_id,
first_name=first_name,
last_name=last_name,
status=status,
comment=comment,
section=section,
recitation=recitation,
email_address=email_address,
user_id=user_id)
def read_data(fname_in):
with open(fname_in, newline='') as f:
reader = csv.reader(f, delimiter=',', quotechar='"')
rows = [Student(Name=row[0], Surname=row[1], Email=row[5]) for row in reader]
return rows
def write_data(fname_out, webworks_list):
#with open(fname_out, 'w', newline='') as f:
with codecs.open(fname_out, 'w', 'ISO-8859-1') as f:
writer = csv.writer(f, delimiter=',',
quotechar='"', quoting=csv.QUOTE_MINIMAL)
for student in webworks_list:
writer.writerow(student)
def main(argv):
fname_in=argv[0]
fname_out=argv[1]
students = read_data(fname_in)
webworks_list = [process_student(s) for s in students]
write_data(fname_out, webworks_list)
if __name__ == '__main__':
from sys import argv
main(argv[1:])
@renegarcia
Copy link
Author

Input: A student list in moodle's format.
Output: List file in webwork2 format.

It is not validated to deal with non english characters, as accents or spanish 'ñ'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment