CSV to Vcard converter

This article was first written in May 2004 for
the BeezNest technical website (http://glasnost.beeznest.org/articles/127)
Here is a Python script I used to convert CSV files containing contact information, extracted from a Microsoft Exchange Server, to vcard format. See also: csv2vcard which is probably better maintained nowadays. TODO:
  • the field names are in Dutch. They should be i18n'ed, with modules for different languages.
Here is the script itself:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# author: stanpinte@fastmail.fm
import os,sys,csv  filename = sys.argv[1]
#note: there seems to be a bug in python csv module...if you don't insert all first fields,
#it doesn't detect them: if you skip field 2, it won't detect field 3, 4, etc.
#--> to report!
# see http://www.python.org/doc/2.3.2/lib/csv-contents.html
#note: we could deduce the fields dictionnary from the first line ;-)
reader = csv.DictReader(file('klanten.csv'),["Titel","Voornaam","Middelste naam","Achternaam","Achtervoegsel","Bedrijf","Afdeling","Functie","Werkadres, straat","Werkadres 2, straat","Werkadres 3, straat","Werkadres, plaats","Werkadres, provincie","Werkadres, postcode","Werkadres, land","Huisadres, straat","Huisadres, straat 2","Huisadres, straat 3","Huisadres, plaats","Huisadres, provincie","Huisadres, postcode","Huisadres, land","Ander adres, straat","Ander adres, straat 2","Ander adres, straat 3","Ander adres, plaats","Ander adres, provincie","Ander adres, postcode","Ander adres, land","Telefoon assistent","Fax op werk","Telefoon op werk","Telefoon op werk 2","Terugbellen","Autotelefoon","Hoofdtelefoon bedrijf","Fax thuis","Telefoon thuis","Telefoon thuis 2","ISDN","Mobiele telefoon","Andere fax","Andere telefoon","Pager","Hoofdtelefoon","Radiotelefoon","Teksttelefoon","Telex","Account","Beroep","Categorieën","Directory-server","E-mailadres","E-mail, weergegeven naam ","E-mailadres 2","E-mail, weergegeven naam 2","E-mailadres 3","E-mail, weergegeven naam 3","Factuurinformatie","Gebruiker 1","Gebruiker 2","Gebruiker 3","Gebruiker 4","Geslacht","Gevoeligheid","Hobby's","Initialen","Kantoorlocatie","Kinderen","Locatie","Naam assistent","Naam manager","Notities","Organisatie-id","Partner","Postbus","Prioriteit","Privé","Referentie van","Reisafstand","Sofi-nummer","Speciale datum","Taal","Trefwoorden","Verjaardag","Vrije/bezette tijden voor Internet-gebruik","Webpagina"])
uid = 0
#skip first line;
next = reader.next()
for next in reader:
  #print next   print "BEGIN:VCARD"
  print "FN:%s %s %s %s %s" % (next['Titel'],next['Voornaam'],next['Middelste naam'],next['Achternaam'],next['Achtervoegsel'])
  print "N:%s %s;%s;%s;%s" % (next['Achternaam'],next['Achtervoegsel'],next['Voornaam'],next['Middelste naam'],next['Titel'])
  if next['E-mailadres']:
  print "EMAIL;INTERNET:%s" % next['E-mailadres']
  if next['E-mailadres 2']:
    print "EMAIL;INTERNET2:%s" % next['E-mailadres 2']
  if next['E-mailadres 3']:
    print "EMAIL;INTERNET3:%s" % next['E-mailadres 3']
  if next['Bedrijf']:
    print "ORG:%s" % next['Bedrijf']
  if next['Functie']:
    print "TITLE:%s" % next['Functie']
  if next['Werkadres 3, straat'] or next['Werkadres 2, straat'] or next['Werkadres, straat']:
    print "ADR;WORK:%s;%s;%s;%s;%s;%s;%s" % (next['Werkadres 3, straat'], next['Werkadres 2, straat'], next['Werkadres, straat'], next['Werkadres, plaats'], next['Werkadres, provincie'], next['Werkadres, postcode'], next['Werkadres, land'])
  if next['Webpagina']:
    print "URL:%s" % next['Webpagina']
  if next['Telefoon op werk']:
    print "TEL;WORK;VOICE:%s" % next['Telefoon op werk']
  if next['Telefoon op werk 2']:
    print "TEL;WORK;VOICE2:%s" % next['Telefoon op werk 2']
  if next['Fax op werk']:
    print "TEL;WORK;FAX:%s" % next['Fax op werk']
  if next['Telefoon thuis']:
    print "TEL;HOME;VOICE:%s" % next['Telefoon thuis']
  if next['Mobiele telefoon']:
    print "TEL;CELL:%s" % next['Mobiele telefoon']
  if next['Autotelefoon']:
    print "TEL;CAR:%s" % next['Autotelefoon']
  if next['Notities']:
    print "NOTE:%s" % next['Notities']
  print "CATEGORIES:%s" % sys.argv[2]
  print "UID:%s" % uid
  print "END:VCARD"   print ""
  uid = uid + 1