How to convert Mailchip-Like subscriber CSV to vCards using Python

If you’ve got a CSV with a header such as

"Email Address","First Name","Last Name",MEMBER_RATING,OPTIN_TIME,OPTIN_IP,CONFIRM_TIME,CONFIRM_IP,LATITUDE,LONGITUDE,GMTOFF,DSTOFF,TIMEZONE,CC,REGION,LAST_CHANGED,LEID,EUID,NOTES

you can use the following Python script to convert it to a directory of vcard files:

#!/usr/bin/env python3
import csv
import os
import argparse

# Set up argument parser
parser = argparse.ArgumentParser(description='Convert a CSV file of contacts to vCard files.')
parser.add_argument('csv_file', type=str, help='Path to the CSV file containing contacts')

args = parser.parse_args()

# Define the path for the CSV file from command line argument
csv_file_path = args.csv_file

# Create a directory to store the vCard files
vcard_directory = 'vcards'
os.makedirs(vcard_directory, exist_ok=True)

# Read the CSV file and process each contact
with open(csv_file_path, mode='r', encoding='utf-8') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    
    for row in csv_reader:
        # Extract contact details
        email = row['Email Address']
        first_name = row['First Name']
        last_name = row['Last Name']
        latitude = row['LATITUDE']
        longitude = row['LONGITUDE']
        notes = row['NOTES']
        
        # Create the vCard content
        vcard_content = (
            f"BEGIN:VCARD\n"
            f"VERSION:3.0\n"
            f"N:{last_name};{first_name};;;\n"
            f"FN:{first_name} {last_name}\n"
            f"EMAIL;TYPE=INTERNET:{email}\n"
        )
        
        if latitude and longitude:
            vcard_content += f"GEO:{latitude};{longitude}\n"
        
        if notes:
            vcard_content += f"NOTE:{notes}\n"
        
        vcard_content += "END:VCARD\n"
        
        # Define the file name for the vCard
        vcard_file_name = f"{first_name}_{last_name}.vcf".replace(" ", "_")
        vcard_file_path = os.path.join(vcard_directory, vcard_file_name)
        
        # Write the vCard content to the file
        with open(vcard_file_path, mode='w', encoding='utf-8') as vcard_file:
            vcard_file.write(vcard_content)

print(f"vCard files have been created in the '{vcard_directory}' directory.")