好吧,希望我能正确理解您要做什么。
你想做这样的事情吗?
#!/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