Skip to content

Instantly share code, notes, and snippets.

@ashinzekene
Last active August 26, 2018 13:43
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 ashinzekene/bc40ca795ab06a9f2b24a74a5cb62296 to your computer and use it in GitHub Desktop.
Save ashinzekene/bc40ca795ab06a9f2b24a74a5cb62296 to your computer and use it in GitHub Desktop.
Generate a vCard(.vcf) from a csvfile
import csv
import re
from datetime import datetime
VCF_TEXT = ''
NAME_SUB = re.compile('( +|,) ?')
with open('./Final Year Bioscope Form.csv', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
lines_read = 0
for row in csv_reader:
strDate = row['Date of Birth']
objDate = datetime.strptime(strDate, '%m-%d')
bday = datetime.strftime(objDate,'%b %d')
if lines_read > 0:
VCF_TEXT += ("BEGIN:VCARD\n"
"VERSION:3.0\n" +
"N:{}\n".format(NAME_SUB.sub(';', row['Name'])) +
"FN:{}\n".format(row['Name'].replace(',', '')) +
"NICKNAME:{}\n".format(row['Nickname']) +
"NOTE:{}\n".format(row['Philosophy of Life']) +
"X-TWITTER:{}\n".format(row['Twitter Handle']) +
"TEL;WORK;VOICE:{}\n".format(row['Phone Number']) +
"BDAY:{}\n".format(bday) +
"CATEGORIES:University of Lagos,CellBiology and Genetics,Helicase 18\n" +
"END:VCARD\n")
lines_read += 1
with open('Final Year Bioscope Form.vcf', 'w+') as vcf_file:
vcf_file.write(VCF_TEXT)
@ashinzekene
Copy link
Author

I used this code to generates a vcard to save my classmates names, nicknames, birthday, phone number, etc from a csv file generated from a google form

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment