vcf2csv and csv2vcf: Converting Android Address Book to and from a Spreadsheet File

(中文版)

I am glad that my wife bought an HTC Aria (running Android) and not an iPhone. So I decided it's worth my time to help her importing her addressbook from a spreadsheet file into her new smart phone. I am sharing three small scripts here so that it's easier for people to convert back and forth between the Android address book file format .vcf and the simple format .csv, which can be more easily edited using a spread sheet program on a computer. For the impatient: uncompress the snapshot and on a linux computer you can:

  1. generate an editable file: ./qp28bit < contacts.vcf | ./vcf2csv > editable.csv
  2. edit editable.csv using oocalc or gnumeric
  3. convert it back for Android to import: ./csv2vcf < editable.csv > new-contacts.vcf

Here are the details:

  1. qp28bit replaces each quoted-printable encoded string (which may span multiple lines) in a .vcf file with 8-bit characters that fits into one line. For example, ./qp28bit contacts.vcf > contacts-8bit.vcf decodes the quoted-printable strings into a new contact file contacts-8bit.vcf so that the utf-8 encoded Chinese characters are readable and editable using any plain text editor. Sometimes I have to apply this more than once to a vcf file exported by Android.
  2. vcf2csv converts a .vcf file containing no multiple-line fields into a spread sheet file. More precisely, (1) it is supposed to process a .vcf file generated by qp28bit, and (2) the PHOTO field is silently dropped! For example, ./vcf2csv contacts-8bit.vcf > editable.csv converts contacts-8bit.vcf into editable.csv, which can be imported into calc (spreadsheet program of OO.o). It accepts the -d option to specify a delimiter other than "," the default delimiter. For example: ./csv2vcf -d '#' contacts.vcf > editable.csv creates a csv file delimited by hash marks instead of commas.
  3. csv2vcf converts a spread sheet file into an Android address book. For example, ./csv2vcf editable.csv > new-contacts.vcf converts editable.csv into new-contacts.vcf . It also accepts the -d option to specify a delimiter other than ",".

These scripts are short and crude, meaning that many assumptions are made about the files, and that there is hardly any error checking. Specifically:

  1. Quotes are not processed. This implies that you cannot have commas in your data, or if you do, you will have to use a delimiter other than comma.
  2. Contents in an input file (to any of the 3 scripts) are assumed to use the utf8 charset. You can see this assumption explicitly in the code of qp28bit. You may have to pre-process the csv file using iconv if your spread sheet program exports csv files in other encodings (big5, gb2312, ... for example).
  3. csv2vcs uses 8-bit encoding instead of the original and more elaborate quoted-printable encoding. Anyway it seems acceptable to Android.
  4. For all scripts, the ouput files uses Linux style newlines -- a newline is one bye (LF) instead of two bytes (CR, LF).

Well, excel can also read/write csv files. But please give OO.o a try next time, ok?

Notes for other Smart Phones and Applications

Different contact managers/address book programs may use slightly different names for the same field. Here is a list of string substitutions to make for smart phones/applications that I happen to have played with.

  1. For gmail to import the vcf file, you have to change all "EMAIL;INTERNET" to "EMAIL;HOME", like this.
  2. For zimbra to import the csv file, you need to use following field names: "firstName", "lastName", "fullName", "homePhone", "mobilePhone", "workPhone", "email", "email2",... like this.

Comments?

I have no energy to maintain a full-blown software package and its corresponding maintainence system. However, your comments are most welcome. Please use this blog entry to leave your comments.

History