Skip to content

Instantly share code, notes, and snippets.

@nomadmonad
Created January 3, 2014 03:26
Show Gist options
  • Save nomadmonad/8232133 to your computer and use it in GitHub Desktop.
Save nomadmonad/8232133 to your computer and use it in GitHub Desktop.
convert vcard (*.vcf) to csv
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'rubygems'
require 'vpim/vcard'
require 'csv'
dest_file = Dir::pwd + "/addresses.csv"
csv = CSV.open(dest_file, "ab")
ARGV.each do |file|
cards = Vpim::Vcard.decode(open(file))
cards.each do |vcard|
addr = vcard.address
phonetic_last_name = if line = vcard.lines("X-PHONETIC-LAST-NAME").first
line.value
else
""
end
phonetic_first_name = if line = vcard.lines("X-PHONETIC-FIRST-NAME").first
line.value
else
""
end
spause = if line = vcard.lines("X-ABRELATEDNAMES").first
line.value
else
""
end
csv << [vcard.name.family,
vcard.name.given,
phonetic_last_name,
phonetic_first_name,
addr.postalcode,
addr.region,
addr.locality,
addr.street,
addr.extended,
spause]
end
end
csv.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment