Search code examples
pythonparsingicalendar

Python iCalendar Attendee format


In trying to parse and .ics file using iCalendar pyton lib I've run into an issue when there are multiple attendees to a meeting. When there are multiple attendees the .get function returns a list of vCalAddress attendees. Is there a way to further parse out the attendees or to return just on item in this list?

from icalendar import Calendar, Event, vDDDTypes, vCalAddress
g = open('Example.ics','rb')
gcal = Calendar.from_ical(g.read())

for event in gcal.walk('vevent'):
    org = event.get('organizer')
    date = event.get('dtstart')
    summary = event.get('summary')
    atten = event.get('attendee')

    print (date.dt)
    print (summary)
    print (org)
    print (atten)
    print ('-----------')

Output

2017-09-29 15:00:00-07:00
FC08F10 Review
mailto:[email protected]
[vCalAddress('mailto:[email protected]'), 
vCalAddress('mailto:[email protected]'), 
vCalAddress('mailto:[email protected]')]
-----------

EDIT I attempted to use the get_line function for the attendees field and through the error messages I received I figured out that vCalAddress is a python list. I edited my print statement to the following print(atten[0]) and for the most part I get what I'm looking for (which is the first result of the attendees list). The one draw back to this at the moment though is that if there is only one attendee you just get the first letter of that attendee's output (if you simply use print(atten[0]) if you use print(atten) with one attendee you get the whole string. I'll keep digging on this.


Solution

  • To get specific attendees when there is more then one you can do the following

    atten = event.get('attendee')
    print(atten[0])
    print(atten[1])
    

    If there is just one attendee there is no need to use the list index just print the variable

    print(atten)
    

    EDIT

    To Parse out multiple attendees do the following:

    for event in gcal.walk('vevent'):
      if atten[i] == 'm':
            print(atten)
        else: 
            print(atten[i])
            ++i
    

    Where 'm' is the first letter of the attendee string. The first letter of you attendee will depend on how its formatted in your .ics file. all my contacts being with 'mailto:'