matlac
matlac

Reputation: 33

How to properly traverse vCards (.vcf) with Python and the vobject module

I have been trying to read a series of vCards and their attributes using the vobject python module. The vCards are not made by me and when I download some, I find some errors in iterating through some conflicting lines. Some of the sections of the .vcfs I am using have minor dependencies that seem to cause errors when iterating through the files contents. Some of these errors are caused by occurrences such as a space, or a parameter taking up multiple lines. Below are examples of vCards that cause issues, as well as the code. In these examples I am trying to get the name, phone number, title, and email for organization purposes.

Example .vcf:

BEGIN:VCARD
VERSION:2.1
N;CHARSET=ISO-8859-1:Example;Name;X.
FN;CHARSET=ISO-8859-1:Name Example X
ORG;CHARSET=ISO-8859-1:Example Org
TITLE;CHARSET=ISO-8859-1:Example Title
TEL;WORK;VOICE:123456789
TEL;WORK;FAX:123456789
ADR;CHARSET=ISO-8859-1;WORK:;;ABCD Tower, Tree Road Street, N.W.;Washington;DC;20006-1037;US
EMAIL;PREF;INTERNET:[email protected]
END:VCARD

BEGIN:VCARD
VERSION:2.1
N;CHARSET=ISO-8859-1:Example;Name;
FN;CHARSET=ISO-8859-1:Name Example
ORG;CHARSET=ISO-8859-1: Example org
TITLE;CHARSET=ISO-8859-1:Example title
TEL;WORK;VOICE:123456789



TEL;WORK;FAX:123456789


ADR;CHARSET=ISO-8859-1;WORK:;;1234 road, NW;Washington;
DC;20036;US

EMAIL;PREF; INTERNET:[email protected]
END:VCARD

Example code:

import vobject
filepath = "a string representing a filepath to a vCard stored as a .vcf"
with open(filepath) as source_file:
    for vcard in vobject.readComponents(source_file):
        print(vcard.fn.value)
        print(vcard.title.value)
        print(vcard.tel.value)
        print(vcard.email.value)

Any sort of way to format a pre-existing vCard file to guarantee it will work with thhe vobject module or any other tool would be a great help as from what I can tell my issue is with reading a formatted vCard. I am very new to using this module, and working with .vcf files in python so any help is greatly appreciated!

Upvotes: 2

Views: 946

Answers (1)

Polluks
Polluks

Reputation: 572

It works

$ python3 vfc.py
Name Example X
Example Title
123456789
[email protected]
Name Example
Example title
123456789
[email protected]

if you remove the illegal line break and space

26,27c26
< ADR;CHARSET=ISO-8859-1;WORK:;;1234 road, NW;Washington;
< DC;20036;US
---
> ADR;CHARSET=ISO-8859-1;WORK:;;1234 road, NW;Washington;DC;20036;US
29c28
< EMAIL;PREF; INTERNET:[email protected]
---
> EMAIL;PREF;INTERNET:[email protected]

However a package maintainer seems to be missing

https://github.com/eventable/vobject/issues

Upvotes: 0

Related Questions