Deprecated: Creation of dynamic property Timber\Site::$pingback is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Site.php on line 180

Deprecated: Creation of dynamic property Timber\User::$name is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/User.php on line 118

Deprecated: Creation of dynamic property Timber\Post::$has_disclaimer is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_edit_lock is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_edit_last is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_meta_box_present is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_send_notification is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_modify_title_and_content is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_notification_custom_heading is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_notification_custom_content is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$tipologia is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_tipologia is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$attributi is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_attributi is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$in_evidenza_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_in_evidenza_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$inviato_in_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_inviato_in_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_category is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_blz_affiliation_attributes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_content_type is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_attributes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_speciali is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_focuskw is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_metadesc is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_linkdex is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_content_score is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_focuskeywords is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_keywordsynonyms is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_estimated-reading-time-minutes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_wordproof_timestamp is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_thumbnail_id is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$response_body is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_indexnow_last_ping is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_oembed_6fe7e382fb602881bdb53ad929612593 is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_oembed_time_6fe7e382fb602881bdb53ad929612593 is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_date_gmt is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$comment_status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$ping_status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_password is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_name is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$to_ping is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$pinged is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_modified is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_modified_gmt is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_content_filtered is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$guid is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$menu_order is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_mime_type is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$comment_count is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$filter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71
Calcolo codice fiscale in locale usando solo Python | IlSoftware.it

Calcolo codice fiscale in locale usando solo Python

Presentiamo uno script Python utile a calcolare in locale il codice fiscale, senza trasferire alcun dato in rete. Le regole per la generazione del codice fiscale, inclusi i dettagli sulla struttura e il significato dei caratteri.

Lo sviluppo di un algoritmo per il calcolo del codice fiscale rappresenta una buona palestra per qualsiasi sviluppatore perché la generazione del codice corretto richiede la comprensione e l’applicazione di regole logiche specifiche. Il codice fiscale, inoltre, è una stringa di caratteri che deve essere formata seguendo un determinato schema. Il programmatore deve perciò cimentarsi con la manipolazione delle stringhe, come la suddivisione, la concatenazione e la trasformazione di caratteri.

L’algoritmo di calcolo del codice fiscale richiede inoltre la validazione di input, come nome, cognome, data e comune di nascita. È quindi fondamentale sapere come gestire e validare i dati in ingresso.

Cos’è il codice fiscale e come si calcola

Il codice fiscale è un identificativo univoco assegnato a ogni cittadino e residente in Italia. È utilizzato per vari scopi, tra cui la registrazione ai servizi pubblici, l’apertura di conti bancari e la dichiarazione dei redditi. Il codice fiscale è composto da 16 caratteri alfanumerici che rappresentano informazioni specifiche riguardo all’individuo, tra cui il nome, il cognome, la data e il luogo di nascita.

Dei 16 caratteri componenti il codice fiscale, i primi 3 rappresentano le consonanti del cognome, con le vocali utilizzate se non ci sono sufficienti consonanti. Se il cognome ha meno di 3 lettere, si completa la sequenza con la lettera “X”. Seguono 3 caratteri per il nome, con uno schema identico a quello utilizzato per il cognome.

Proseguendo verso destra, si hanno due caratteri che rappresentano le ultime due cifre dell’anno di nascita; un carattere per l’anno di nascita (secondo la tabella pubblicata a questo indirizzo); due caratteri per il giorno di nascita (se la persona è uomo, si utilizza il giorno di nascita così com’è; se donna, si aggiunge 40 al giorno di nascita).

I successivi 4 caratteri indicano il codice del comune di nascita, altrimenti noto come codice Belfiore: la lista dei codici univoci previsti per ciascun comune d’Italia è pubblicata nella pagina Elenco codici dei comuni italiani.

L’ultimo carattere si chiama carattere di controllo: utilizza i primi 15 caratteri per determinare un valore che aiuta a verificare la validità del codice fiscale. Se ne parla nel dettaglio in questa pagina: le tabelle per i caratteri alfanumerici pari e dispari sono esattamente quelle che abbiamo implementato nella funzione calcola_carattere_di_controllo().

Come calcolare il codice fiscale con Python, in locale

Abbiamo realizzato uno script Python che provvede a calcolare il codice fiscale in locale, senza trasferire alcun dato su server remoti.

Come primo passo, l’applicazione verifica la presenza di un file comuni.txt in locale. In sua mancanza, provvede a scaricare dal sito del Ministero tutti i nomi dei comuni italiani insieme con i rispettivi codici Belfiore. Nomi dei comuni e codici corrispondenti, sono salvati nel file comuni.txt.

La funzione calcola_codice_fiscale() richiede all’utente di inserire i dati anagrafici (nome, cognome, data di nascita, sesso e comune di nascita) e provvede quindi a invocare calcola_codice_fiscale_utente() per generare il codice fiscale. La funzione estrae le consonanti e le vocali dai nomi e calcola il codice fiscale in base alle regole fissate.

Per eseguire lo script, basta scaricare questo codice Python quindi salvarlo in locale come cf.py. Dando per scontato che Python sia correttamente installato, basta digitare python3 cf.py per avviarlo.

Installazione delle librerie Python necessarie

Per eseguire lo script, è necessario avere installato Python e alcune librerie specifiche. Per procedere in tal senso, basta utilizzare il comando pip:

pip install requests beautifulsoup4

Utilizziamo la libreria requests per effettuare richieste HTTP; beautifulsoup4 per il parsing dei contenuti HTML. Entrambi i componenti software sono utili per scaricare la lista dei codici dei comuni dal sito del Ministero e salvarla sotto forma di file di testo.

Decodifica dei codici fiscali ovvero calcolo del codice fiscale inverso

Lo script che abbiamo sviluppato permette non soltanto di calcolare il codice fiscale, ma anche di elaborare il codice fiscale inverso. In altre parole, a partire da un codice fiscale, il codice Python estrae sesso, data di nascita e luogo di nascita. Nomi e cognomi, invece, sono al momento lasciati nella loro forma composta da tre lettere. Questo perché, evidentemente, agli stessi gruppi di nomi e cognomi formati da tre lettere possono corrispondere più alternative.

Ciò che si può fare, in un futuro aggiornamento dello script, è raccogliere nomi e cognomi dei cittadini italiani quindi fornire una stima delle combinazioni verosimilmente più probabili.

La funzione codice_fiscale_inverso() permette di risalire ai dati anagrafici originali partendo da un codice fiscale. Utilizza calcola_fiscale_inverso() per analizzare il codice fiscale inserito e restituire i dati corrispondenti.

Come usare lo script Python per il calcolo del codice fiscale

Dopo aver avviato lo script Python (lo si può fare anche da Windows utilizzando WSL, Sottosistema Windows per Linux), il codice effettua dapprima (“una tantum”) il download dei codici Belfiore e dei nomi dei comuni. Chiede quindi se si volesse calcolare un codice fiscale oppure decodificarne uno.

Calcolo codice fiscale con Python in locale

L’applicazione chiede di inserire i vari dati anagrafici per poi fornire il responso finale ovvero il codice fiscale calcolato sulla base dei dati anagrafici inseriti.

Al momento lo script non è in grado di gestire i codici fiscali di cittadini nati all’estero. L’elaborazione è limitata ai soli codici italiani.

Ti consigliamo anche

Link copiato negli appunti