Skip to content

Instantly share code, notes, and snippets.

@gnarula
Last active December 28, 2015 09:29
Show Gist options
  • Save gnarula/7479359 to your computer and use it in GitHub Desktop.
Save gnarula/7479359 to your computer and use it in GitHub Desktop.
A python script to parse CHEM F111 midesem grades to a sqlite3 database
"""
Instructions:
1. Convert the pdf to a txt file using `pdftotext -layout chem.pdf out.txt`
2. Remove the names of absentees and ensure the results of each student are on 1 line
3. Create data.db with the following schema `CREATE TABLE chem (id TEXT, name TEXT, section TEXT, t1 REAL, t2 REAL, midsem REAL, grade TEXT, gp INT);`
"""
import re
import sqlite3
def gp(grade):
if grade == 'A':
return 10
elif grade == 'A-':
return 9
elif grade == 'B':
return 8
elif grade == 'B-':
return 7
elif grade == 'C':
return 6
elif grade == 'C-':
return 5
elif grade == 'D':
return 4
elif grade == 'E':
return 2
def grade(marks):
marks = float(marks)
if marks >= 98:
return 'A'
elif 98 > marks >= 89:
return 'A-'
elif 89 > marks >= 78:
return 'B'
elif 78 > marks >= 66:
return 'B-'
elif 66 > marks >= 57:
return 'C'
elif 57 > marks >= 49:
return 'C-'
elif 49 > marks >= 31:
return 'D'
return 'E'
with open('out.txt', 'r') as f:
content = f.read()
regex = re.compile("(201(3|2)(A|B)[0-9]PS[0-9]{3}G)\s+([A-Z ]+)\s+(T[0-9])\s+([0-9.]+)\s+([0-9.]+)\s+([0-9.]+)")
students = regex.findall(content)
with sqlite3.connect('data.db') as con:
cur = con.cursor()
for student in students:
val = list(student)
del val[1:3]
val.append(grade(val[-1]))
val.append(gp(val[-1]))
cur.execute("INSERT INTO chem VALUES (?,?,?,?,?,?,?,?)", val)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment