MonScoDocEssai/misc/get_codes_from_names.py

71 lines
1.8 KiB
Python
Raw Normal View History

2020-09-26 16:19:37 +02:00
#!/usr/bin/env python
"""Pour un semestre, Affiche colonnes code_nip, code_ine
etant donnes les noms/prenoms dans un CSV
(ne change pas la BD)
XXX TODO: OBSOLETE, a moderniser (psycopg2, python 3, encoding)
"""
2021-07-12 11:54:04 +02:00
import pdb, os, sys, psycopg
2020-09-26 16:19:37 +02:00
import csv
2021-07-12 11:54:04 +02:00
CSVFILENAME = "/tmp/aaa.csv"
formsemestre_id = "SEM229"
DBCNXSTRING = "host=localhost user=scoinfo dbname=SCOINFO password=XXX"
2020-09-26 16:19:37 +02:00
idx_prenom = 1
idx_nom = 0
# en general, pas d'accents dans le CSV
2021-07-12 11:54:04 +02:00
SCO_ENCODING = "iso8859-15"
# from SuppressAccents import suppression_diacritics
# XXX a revoir si ce script est utile: en python3, unicodedata.normalize("NFD", s).encode("ascii", "ignore").decode(SCO_ENCODING)
2020-09-26 16:19:37 +02:00
def suppr_acc_and_ponct(s):
2021-07-12 11:54:04 +02:00
s = s.replace(" ", "")
s = s.replace("-", " ")
return str(suppression_diacritics(unicode(s, SCO_ENCODING)))
2020-09-26 16:19:37 +02:00
def make_key(nom, prenom):
2021-07-12 11:54:04 +02:00
nom = suppr_acc_and_ponct(nom).upper()
2020-09-26 16:19:37 +02:00
prenom = suppr_acc_and_ponct(prenom).upper()
2021-07-12 11:54:04 +02:00
return nom + " " + prenom[:4]
2020-09-26 16:19:37 +02:00
2021-07-12 11:54:04 +02:00
reader = csv.reader(open(CSVFILENAME, "rb"))
2020-09-26 16:19:37 +02:00
noms = {}
for row in reader:
2021-07-12 11:54:04 +02:00
if row[0][0] != "#":
key = make_key(row[idx_nom], row[idx_prenom])
2020-09-26 16:19:37 +02:00
if noms.has_key(key):
2021-07-12 11:54:04 +02:00
raise ValueError, "duplicate key: %s" % key
2020-09-26 16:19:37 +02:00
noms[key] = row
2021-07-12 11:54:04 +02:00
cnx = psycopg.connect(DBCNXSTRING)
2020-09-26 16:19:37 +02:00
cursor = cnx.cursor()
2021-07-12 11:54:04 +02:00
cursor.execute(
"select * from identite i, notes_formsemestre_inscription ins where i.etudid = ins.etudid and ins.formsemestre_id = '%s'"
% formsemestre_id
)
2020-09-26 16:19:37 +02:00
R = cursor.dictfetchall()
2021-07-12 11:54:04 +02:00
nok = 0
print "nom,prenom,ine,nip"
2020-09-26 16:19:37 +02:00
for e in R:
2021-07-12 11:54:04 +02:00
key = make_key(e["nom"], e["prenom"])
2020-09-26 16:19:37 +02:00
if not noms.has_key(key):
2021-07-12 11:54:04 +02:00
print "** no match for %s (%s)" % (key, e["etudid"])
2020-09-26 16:19:37 +02:00
else:
info = noms[key]
2021-07-12 11:54:04 +02:00
print "%s,%s,%s,%s" % (e["nom"], e["prenom"], e["code_ine"], e["code_nip"])
nok += 1
2020-09-26 16:19:37 +02:00
cnx.commit()
2021-07-12 11:54:04 +02:00
print "%d etudiants, %d ok" % (len(R), nok)