0
    #!/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 但它不会因为 vcards 被认为是不同的,即使它们是相同的。最终目标之一是找到一种从 vcard 文件中删除重复项的方法。加分点:有没有办法使比较兼容使用一种快速方法来统一 Python 中的列表,例如:

    set(ablist) 

删除重复项?(例如,以某种方式将电子名片转换为字符串......)。在上面的代码中 len(set(ablist)) 返回 2 而不是 1 如预期的那样......

相反,如果不是比较整个 vcard,而是比较它的一个组件,如下所示:

    print ablist[0].fn==ablist[1].fn

然后我们确实看到了预期的行为并收到 True 作为响应...

这是测试中使用的文件内容(只有两个相同的 vcard):

    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
4

2 回答 2

1

@Brian Barcelona,关于您的回答,只是为了让您知道,而不是:

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 的不同组件可能相同但顺序不同。我认为最好的方法是您自己检查每个相关组件。

于 2017-01-04T23:15:59.480 回答
0

我发现以下方法可行 - 洞察力是“序列化()”电子卡:

#!/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].serialize()==ablist[1].serialize()

但是,应该有更好的方法来做到这一点......任何帮助都会受到欢迎!

于 2017-01-04T15:32:38.690 回答