Last active
August 26, 2018 13:43
-
-
Save ashinzekene/bc40ca795ab06a9f2b24a74a5cb62296 to your computer and use it in GitHub Desktop.
Generate a vCard(.vcf) from a csvfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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