【问题标题】:To remove vcard contact duplicates, comparing if two vcards are equal in .vcf file does not work with simple == vobject comparison要删除 vcard 联系人重复项,比较 .vcf 文件中的两个 vcard 是否相等不适用于简单 == vobject 比较
【发布时间】:2017-01-04 09:12:36
【问题描述】:
    #!/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

【问题讨论】:

    标签: python-2.7 comparison vcf-vcard vobject


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

    【讨论】:

    • 非常感谢您的评论。我不知道如何访问所有组件,有没有简单的方法?例如,我不知道如何访问注释字段。我开发了一个代码来检查重复,它做得很好,但正如你所说,它遗漏了一些......如果我可以访问所有组件,那么如果某些字段(如电子邮件或名称)相同,我也可以建议合并。 .. 任何帮助都将受到欢迎...
    【解决方案2】:

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

    #!/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()
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-26
      相关资源
      最近更新 更多