#!/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()
上面的代码不起作用,但是如果我们删除其中的两个重复语句中的一个。我想逐个阅读vcards。有没有办法用vobject.readOne(f)做到这一点?如何从文件中读取vcard n?
我使用的vcf文件来自谷歌联系人(以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读数进行比较以避免重复,如另一个问题所示:
答案 0 :(得分:0)
好吧,我希望我能正确理解你的目标。
你想做这样的事吗?
#!/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