不会从 .vcf 文件中读取多个 vcard,vobject.readOne(f) 仅适用于第一个 vcard

Won't read multiple vcards from .vcf file, vobject.readOne(f) only works the first vcard

    #!/usr/bin/env python2.7

    import vobject

    abfile='/foo/bar/directory/file.vcf'

    def test_vobject_dot_readOne():
        with open(abfile) as f:
            vcard = vobject.readOne(f)
            vcard = vobject.readOne(f)

    test_vobject_dot_readOne()

上面的代码不起作用,但如果我们删除两个重复语句中的一个,它就会起作用。我想一张一张地阅读电子名片。有没有办法用 vobject.readOne(f) 做到这一点?如何从文件中读取 vcard n?

我使用的 vcf 文件取自 google 联系人(导出为 vcard 格式)。这是我在测试中使用的只有两个 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_bar2 
    N:;Foo_bar2;;;
    EMAIL;TYPE=INTERNET:foobar2@foo.bar.com
    END:VCARD

然后可以使用 Vcard 读数进行比较以避免重复,如另一个问题所示:

好吧,希望我能正确理解您的意图。

你想做这样的事情吗?

#!/usr/bin/env python2.7

import vobject

test_vcard_information = r"""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_bar2
N:;Foo_bar2;
EMAIL;TYPE=INTERNET:foobar2@foo.bar.com
END:VCARD
"""

for vcard in vobject.readComponents(test_vcard_information):
    print vcard.fn.value

或者如果您有文件中的信息:

#!/usr/bin/env python2.7

import vobject

test_filename = r'/path/to/Test_addressbook.vcf'

with open(test_filename) as source_file:
    for vcard in vobject.readComponents(source_file):
       print vcard.fn.value

您可能会将 .readOne() 与日历文件 (.ics) 一起使用,因为它将由单个根对象组成,如本例所示:

#!/usr/bin/env python2.7

import vobject

test_calendar_information = r"""BEGIN:VCALENDAR
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN
VERSION:2.0
BEGIN:VTIMEZONE
TZID:America/Toronto
BEGIN:DAYLIGHT
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
TZNAME:EDT
DTSTART:19700308T020000
RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
TZNAME:EST
DTSTART:19701101T020000
RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
CREATED:20170104T022518Z
LAST-MODIFIED:20170104T022643Z
DTSTAMP:20170104T022643Z
UID:3fab09d6-59bb-430b-8b21-56c9636871e2
SUMMARY:Write a chapter
CATEGORIES:Projects
DTSTART;TZID=America/Toronto:20170105T140000
DTEND;TZID=America/Toronto:20170105T150000
TRANSP:OPAQUE
X-MOZ-GENERATION:2
LOCATION:At home
DESCRIPTION:One day I will be a great writer but I have to start somewhere...
SEQUENCE:1
END:VEVENT
BEGIN:VEVENT
CREATED:20170104T022346Z
LAST-MODIFIED:20170104T022654Z
DTSTAMP:20170104T022654Z
UID:b304f46a-f533-4aa4-8ee1-3b59649dedfa
SUMMARY:See a movie
CATEGORIES:Entertainment
DTSTART;TZID=America/Toronto:20170103T110000
DTEND;TZID=America/Toronto:20170103T140000
TRANSP:OPAQUE
X-MOZ-GENERATION:4
LOCATION:Somewhere over the rainbow
DESCRIPTION:The Wizard of Oz is movie I haven't seen in a long time.\n\nWe
  should schedule a time to see it
SEQUENCE:1
END:VEVENT
END:VCALENDAR"""

vcalendar = vobject.readOne(test_calendar_information)
for vevent in vcalendar.vevent_list:
    print vevent.summary.value

或者如果它在文件中:

#!/usr/bin/env python2.7

import vobject

test_filename = r'/path/to/Test_Calendar.ics'

with open(test_filename) as source_file:
    vcalendar = vobject.readOne(source_file)
    for vevent in vcalendar.vevent_list:
        print vevent.summary.value