Search code examples
pythonicalendar

Python iCalendar parse data and write to another ics file


I am newbie in Python and I'm trying to parse and .ics file using iCalendar python lib. I want to filter and write events where attendee is 10059707 into a new file named example.ics. When trying print, it still prints 2 events as shown below but only writes the last 1 event to the new file

from datetime import datetime
from pytz import UTC # timezone


g = open('Calendar_元データ.ics','rb')
gcal = Calendar.from_ical(g.read())
for component in gcal.walk():
    if component.name == "VEVENT":
      DTSTAMP = component.get('DTSTAMP')
      UID = component.get('UID')
      ATTENDEE = component.get('ATTENDEE')
      CREATED = component.get('CREATED')
      SUMMARY = component.get('SUMMARY')
      DTSTART = component.get('DTSTART').dt    
      DTEND = component.get('DTEND').dt 
      LOCATION = component.get('LOCATION')    
      CLASS = component.get('CLASS')    
      DESCRIPTION = component.get('DESCRIPTION')    
      PRIORITY = component.get('PRIORITY')    
      ORGANIZER = component.get('ORGANIZER')    
      LAST_MODIFIED = component.get('LAST-MODIFIED')    
      X_NAVER_CATEGORY_ID = component.get('X-NAVER-CATEGORY-ID')    
      TRIGGER = component.get('TRIGGER')    
      ACTION = component.get('ACTION')    


      cal2 = Calendar()
      event2 = Event()

      if ATTENDEE is not None and '10059707' in ATTENDEE:
            
        print ("1:{} \n 2:{} \n 3:{} \n 4:{} \n 5:{} \n 6:{} \n 7:{} \n 8:{} \n 9:{} \n 10:{} \n 11:{} \n 12:{} \n 13:{}\n  14:{}\n 15:{}\n 16:{}\n".format(DTSTAMP,UID,ATTENDEE,CREATED,SUMMARY,DTSTART,DTEND,LOCATION,CLASS,DESCRIPTION,PRIORITY,ORGANIZER,LAST_MODIFIED,X_NAVER_CATEGORY_ID,TRIGGER,ACTION))
        print(ATTENDEE)

        for event3 in component.walk():
          if event3.name == "VEVENT":
            
            event2.add('DTSTAMP', DTSTAMP)
            event2.add('UID', UID)
            event2.add('attendee',vCalAddress(ATTENDEE))
            event2.add('CREATED', CREATED)
            event2.add('summary', "作業予定あり")
            event2.add('DTSTART', DTSTART)
            event2.add('DTEND', DTEND)
            event2.add('LOCATION', LOCATION)
            event2.add('CLASS', CLASS)
            event2.add('DESCRIPTION', SUMMARY + " " + DESCRIPTION)
            event2.add('PRIORITY', PRIORITY)
            event2.add('ORGANIZER',  vCalAddress(ORGANIZER))
            event2.add('LAST-MODIFIED', LAST_MODIFIED)
            event2.add('X-NAVER-CATEGORY-ID', X_NAVER_CATEGORY_ID)
            event2.add('ACTION', ACTION)

            cal2.add_component(event2)
            f = open('example.ics','wb')
            f.write(cal2.to_ical())
            f.close()


g.close()

[Output][1][enter image description here][2]

1:<icalendar.prop.vDDDTypes object at 0x0000025CE05284F0>
 2:[email protected]
 3:https://calendar.worksmobile.com/resources/resource/10059707/10059707@0e894991-6dce-4a3d-b84d-24b4935784fd 
 4:<icalendar.prop.vDDDTypes object at 0x0000025CE0528760>
 5:西村 テスト1 作業予約
 6:2021-10-01 13:00:00+09:00
 7:2021-10-01 14:00:00+09:00
 8:
 9:PUBLIC
 10:メモメモ
 11:0
 12:mailto:38dab8c1-99f7-40a2-964f-0f716d9c7d2b@jp1-groups.calendar.worksmobile.com
 13:<icalendar.prop.vDDDTypes object at 0x0000025CE05288E0>
  14:0
 15:None
 16:None

https://calendar.worksmobile.com/resources/resource/10059707/10059707@0e894991-6dce-4a3d-b84d-24b4935784fd
1:<icalendar.prop.vDDDTypes object at 0x0000025CE0528A30>
 2:[email protected]
 3:https://calendar.worksmobile.com/resources/resource/10059707/10059707@0e894991-6dce-4a3d-b84d-24b4935784fd
 4:<icalendar.prop.vDDDTypes object at 0x0000025CE0528CA0>
 5:西村テスト2 作業予定3
 6:2021-10-08 14:00:00+09:00
 7:2021-10-08 15:00:00+09:00
 8:
 9:PUBLIC
 10:メモメモ2
 11:0
 12:mailto:38dab8c1-99f7-40a2-964f-0f716d9c7d2b@jp1-groups.calendar.worksmobile.com
 13:<icalendar.prop.vDDDTypes object at 0x0000025CE0528E20>
  14:0
 15:None
 16:None

https://calendar.worksmobile.com/resources/resource/10059707/10059707@0e894991-6dce-4a3d-b84d-24b4935784fd````

Solution

  • You are opening the example.ics file in write mode, which overrides existing content if there was any, and write new one, which justifies why you're only getting one entry, and I guess that entry should be the last iteration of your loop.

    One solution could be opening the file in append mode with a or a+.

    Another solution could be simply opening your file outside the loop - before the line for event3 in component.walk():. This way, you would keep the file open while you are in the loop, and write all you need to write. Then close the file at the end of the loop (outside the loop too).