Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert a CSV file of people to vCard files for updating iOS contacts
"""
Convert a CSV file of the form
```
name, email, cell
First Last, bob@example.com, 800-555-1234
...
```
to vCard (vcf) files suitable for creating/updating iOS contacts.
Based on https://codereview.stackexchange.com/questions/3517/converting-a-csv-to-vcards-in-python
but updated for Python 3 and for my use case.
"""
import csv
def convert(file):
reader = csv.DictReader(file)
template = """\
BEGIN:VCARD
VERSION:3.0
N:{last};{first}
FN:{first} {last}
EMAIL;type=INTERNET;type=HOME;type=pref:{email}
TEL;type=CELL;type=VOICE;type=pref:{cell}
END:VCARD\
"""
for row in reader:
first, last = row['name'].split()
email = row['email']
cell = row['cell']
data = dict(first=first, last=last, email=email, cell=cell)
with open("{first} {last}.vcf".format(**data), "w") as f:
f.write(template.format(**data))
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description="Convert CSV to a bunch of vCard files.")
parser.add_argument(
"file", type=argparse.FileType("r"), help="CSV file to convert")
args = parser.parse_args()
convert(args.file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment