要删除vcard联系人重复项,比较.vcf文件中两个vcard是否相等不能通过简单== vobject比较进行工作 [英] To remove vcard contact duplicates, comparing if two vcards are equal in .vcf file does not work with simple == vobject comparison
问题描述
#!/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]
上面的代码应该返回True,但这不是因为vcard即使相同也被认为是不同的.最终目标之一是找到一种从vcard文件中删除重复项的方法.优点:有没有一种方法可以使比较与使用以下一种快速方法来使Python列表唯一化的方式兼容:
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)
要删除重复项? (例如,以某种方式将vcard转换为字符串...).在上面的代码中,len(set(ablist))返回2而不是预期的1.
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...
相反,如果不是比较整个vcard,而是比较它的一个组成部分,如下所示:
In contrast, if instead of comparing the whole vcard we compare one component of it as in:
print ablist[0].fn==ablist[1].fn
然后,我们确实会看到预期的行为并收到True作为响应...
then we do see the expected behavior and receive True as response...
这是测试中使用的文件内容(只有两个相同的vcard):
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:foobar1@foo.bar.com
END:VCARD
BEGIN:VCARD
VERSION:3.0
FN:Foo_bar1
N:;Foo_bar1;;;
EMAIL;TYPE=INTERNET:foobar1@foo.bar.com
END:VCARD
推荐答案
@Brian Barcelona,关于您的答案,只是为了让您知道,而不是:
@Brian Barcelona, concerning your answer, just to let you know, instead of:
ablist = []
with open(abfile) as source_file:
for vcard in vobject.readComponents(source_file):
ablist.append(vcard)
您可以这样做:
with open(abfile) as source_file:
ablist = list(vobject.readComponents(source_file))
顺便说一句,我已经查看了该模块的源代码,由于vcard的不同组件可能相同但顺序不同,因此不能保证您的解决方案能正常工作.我认为最好的方法是让您自己检查每个相关组件.
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.
这篇关于要删除vcard联系人重复项,比较.vcf文件中两个vcard是否相等不能通过简单== vobject比较进行工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!