#!/usr/bin/env python2.7
import vobject
abfile='/foo/bar/directory/file.vcf' #ab stands for address book
ablist = []
with open(abfile) as source_file:
for vcard in vobject.readComponents(source_file):
ablist.append(vcard)
print ablist[0]==ablist[1]
The above code should return True but it does not because the vcards are considered different even though they are the same. One of the ultimate objectives is to find a way to remove duplicates from the vcard file. Bonus points: Is there a way to make the comparison compatible with using one of the fast ways to uniqify a list in Python such as:
set(ablist)
to remove duplicates? (e.g. convert the vcards to strings somehow...). In the code above len(set(ablist)) returns 2 and not 1 as expected...
In contrast, if instead of comparing the whole vcard we compare one component of it as in:
print ablist[0].fn==ablist[1].fn
then we do see the expected behavior and receive True as response...
Here is the file contents used in the test (with only two identical vcards):
BEGIN:VCARD
VERSION:3.0
FN:Foo_bar1
N:;Foo_bar1;;;
EMAIL;TYPE=INTERNET:[email protected]
END:VCARD
BEGIN:VCARD
VERSION:3.0
FN:Foo_bar1
N:;Foo_bar1;;;
EMAIL;TYPE=INTERNET:[email protected]
END:VCARD
@Brian Barcelona, concerning your answer, just to let you know, instead of:
You could do:
By the way, I have looked in the source code of this module and your solution is not guaranteed to work because different components of a vcard could be the same but not in the same order. I think the best way is for you to check each relevant component yourself.