How I got my cousemates' birthday dates 
Tags: productivity, python, csv, vcf
Published: 2018-08-26 13:19:22

---
title: How I got my cousemates' birthday dates 
published: true
description: Converting csv to vcf to get birthdays and contact information
tags: productivity,python,csv,vcf
---

My love for automation and looking for shortcuts at it again. I've always wanted to have all my classmates names (Surname and Firstname) in my address book and their birthday dates so I can send them birthday wishes even afer leaving school. A google form was created for us to fill in our profile(final year stuff), I had access to it, so I downloaded the form in csv. Of course I could have manually created entries for each of the 86 of them, but this would have been time consuming. I did some research on `vCard` and `vcf` files using these resources https://en.wikipedia.org/wiki/VCard and https://tools.ietf.org/html/rfc6350. I found out that the basic format for a vcf is like:

```vcf
BEGIN:VCARD
VERSION:2.1
N:Gump;Forrest;;Mr.
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TEL;HOME;VOICE:(404) 555-1212
ADR;WORK;PREF:;;100 Waters Edge;Baytown;LA;30314;United States of America
LABEL;HOME;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:42 Plantation St.=0D=0A=
 Baytown, LA 30314=0D=0AUnited States of America
EMAIL:forrestgump@example.com
END:VCARD
```

so I wrote a code in python so create a vcard entry for each classmate from the csv file which had the following rows

```csv
"Name","Nickname","Date of Birth","State of Origin","Phone Number","Twitter Handle","Instagram Handle","Snapchat handle","Philosophy of Life","Best Department Course","Worst Department Course"...
```

Here's the code:

 ```py
import csv

VCF_TEXT = ''

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:
        if lines_read > 0:
            VCF_TEXT += ("BEGIN:VCARD\n"
            "VERSION:3.0\n" +
            "N:{}\n".format(row['Name'].replace(',', '')) +
            "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(row['Date of Birth']) +
            "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)
```

One challenge I had was the `N` field from the CSV required that the names be seperated by semicolon(;) but some people had their names seperated by a comma (,) while other used a whitespace, so I used this regular expression to solve my issue

```py
import re

NAME_SUB = re.compile('( +|,) ?')
NAME_SUB.sub(';', row['Name'])
```

The code replaces commas and spaces and its different combinations with the appropriate delimiter.

Next issue was some with the birthday date. Google saved birthdays in this format

```vcf
BDAY: Jul 23
```

but I got this format from the csv `06-23` so I had to convert it to the appropriate format. This helped

```py
from datetime import datetime

strDate = row['Date of Birth']
objDate = datetime.strptime(strDate, '%m-%d')
bday = datetime.strftime(objDate,'%b %d')
```
It reads the birthday date from the csv and converts it to the required format. At the end the reformed code looked like this

 ```py
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)
```

After running the code. It generated the vcf for me. All I then did was import the vcf file into google contacts. This can be done on mobile and on the web. And _hey presto_ I had all my classmates full names, phone numbers, birthday dates and twitter handles.