Created
February 27, 2018 17:16
-
-
Save Recursing/a81b74595f04324dcd82aeb2cbb3469c to your computer and use it in GitHub Desktop.
Estrai informazioni dal codice fiscale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import namedtuple | |
from datetime import date | |
Person = namedtuple('Person', 'cognome nome data_nascita sesso comune') | |
def reverse_cf(cf: str) -> Person: | |
assert len(cf) == 16 | |
cf = cf.upper() | |
cognome = cf[:3] | |
nome = cf[3:6] | |
anno = int(cf[6:8]) | |
if anno <= date.today().year % 100: | |
anno += 2000 | |
else: | |
anno += 1900 | |
mappa_mesi = 'ABCDEHLMPRST' | |
mese = mappa_mesi.index(cf[8]) + 1 | |
giorno = int(cf[9:11]) % 40 | |
sesso = 'M' if int(cf[9:11]) < 40 else 'F' | |
data_nascita = date(anno, mese, giorno) | |
# vedi http://www.comuni-italiani.it/catastale/ | |
comune = cf[11:15] | |
return Person(cognome, nome, data_nascita, sesso, comune) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment