Skip to content

Instantly share code, notes, and snippets.

@Recursing
Created February 27, 2018 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Recursing/a81b74595f04324dcd82aeb2cbb3469c to your computer and use it in GitHub Desktop.
Save Recursing/a81b74595f04324dcd82aeb2cbb3469c to your computer and use it in GitHub Desktop.
Estrai informazioni dal codice fiscale
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