Delete duplicate contacts and contacts with more or less than X digits using Python
If you export the address book in CSV, you can delete duplicate contacts and also those with phone numbers with more or less than X digits.
First you need to export your contacts in CSV format.
For this tutorial we use python.
First you need to read the csv file:
contacts = pd.read_csv("contacts.csv")
To delete the contacts with the duplicate phone number do this command. You have to replace “Numero di telefono” with the name of the phone number column of your CSV file:
contacts.drop_duplicates(subset=['Numero di telefono'])
# name of the column that identifies the telephone number
If you want to delete duplicate contacts based on multiple columns:
contacts.drop_duplicates(subset=['A', 'B'], keep= 'last')
To delete contacts with more or less than X digits because they are invalid contacts.
# clears numbers that have more than 13 digitsindex_to_drop = contacts[contacts['Numero di telefono'].map(len) != 13].indexcontacts.drop(index_to_drop, inplace=True)
After the various deletions of duplicate or invalid contacts, let’s go to save the CSV.
# save in a new csv
contacts.to_csv('contacts_new.csv', index = False)
Now you can import the created CSV into your address book management system.
I’ll put below the complete code I used:
If you have any problems or if you have solved them in any other way do not hesitate to write them in the comments!