Skip to content

Instantly share code, notes, and snippets.

@carlos22
Created July 20, 2011 17:52
Show Gist options
  • Save carlos22/1095487 to your computer and use it in GitHub Desktop.
Save carlos22/1095487 to your computer and use it in GitHub Desktop.
LSF Noten Mapping
#!/usr/bin/python
#
# LSF Noten Mapping
#
# use openoffice or catdocs xls2csv [sudo apt-get install catdoc] - note: its broken in ubuntu natty
#
import csv
import sys
DELIMITER=','
QUOTECHAR='"'
def readMarks(infile):
print "Start reading marks from %s..." % infile
markMapping = {}
markReader = csv.reader(open(infile, 'rb'), delimiter=DELIMITER, quotechar=QUOTECHAR)
for row in markReader:
matNo = row[1]
if(matNo.isdigit()):
print 'mapped %s -> %s' % (matNo,row[9])
markMapping[matNo] = row[9]
return markMapping
def writeMarks(outfile, markMapping):
print "\n-----------------------------"
print "Processing %s " % outfile
print "-----------------------------"
handledMatNos = []
lsfReader = csv.reader(open(outfile, 'rb'), delimiter=DELIMITER, quotechar=QUOTECHAR)
lsfWriter = csv.writer(open(outfile+'.graded.csv', 'wb'), delimiter=DELIMITER, quotechar=QUOTECHAR) #quoting=csv.QUOTE_MINIMAL)
for row in lsfReader:
matNo = row[7]
if(matNo.isdigit()):
handledMatNos.append(matNo)
mark = '5U'
try:
mark = markMapping[matNo]
except Exception:
print 'WARNING: %s not found, assuming 5U' % matNo
row[11] = mark
lsfWriter.writerow(row)
return handledMatNos
def main():
if len(sys.argv) < 3 :
print "Transfer Marks to LSF"
print "usage: %s marks.csv nv_lsfno1.csv nv_lsfno2.csv " % sys.argv[0]
return -1
infile = sys.argv[1]
outfiles = sys.argv[2:]
markMapping = readMarks(infile)
allHandledMatNos = []
for outfile in outfiles:
allHandledMatNos += writeMarks(outfile,markMapping)
print "-----------------------------"
matNosUsedInInput = markMapping.keys()
notHandledMatNos = [no for no in matNosUsedInInput if no not in allHandledMatNos]
if len(notHandledMatNos) > 0:
print '\nThe following numbers wheren\'t added to any of the processed files, and are not rated as 5U'
notHandledMatNosStr = ""
for matNo in notHandledMatNos:
mark = markMapping[matNo]
if mark != '5U':
notHandledMatNosStr += matNo + "(%s), " % mark
print notHandledMatNosStr
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment