Python iCalendar 解析数据并写入另一个 ics 文件
Python iCalendar parse data and write to another ics file
我是 Python 的新手,我正在尝试使用 iCalendar python 库解析 .ics 文件。我想过滤 attendee 为 10059707 的事件并将其写入名为 example.ics 的新文件中。尝试打印时,它仍然打印 2 个事件,如下所示,但只将最后 1 个事件写入新文件
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()
[输出][1][在此处输入图像描述][2]
1:<icalendar.prop.vDDDTypes object at 0x0000025CE05284F0>
2:20211011T034853Z-131@jvcweb02.wcal.nfra.io
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:20211011T034941Z-175@jvcweb02.wcal.nfra.io
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````
您正在以写入模式打开 example.ics
文件,这会覆盖现有内容(如果有)并写入新内容,这证明了为什么您只获得一个条目,我想该条目应该是循环的最后一次迭代。
一种解决方案是使用 a
或 a+
以附加模式打开文件。
另一种解决方案可能只是在循环外打开文件 - 在 for event3 in component.walk():
行之前。这样,您将在循环中保持文件打开,并写入所有需要写入的内容。然后在循环结束时关闭文件(也在循环外)。
我是 Python 的新手,我正在尝试使用 iCalendar python 库解析 .ics 文件。我想过滤 attendee 为 10059707 的事件并将其写入名为 example.ics 的新文件中。尝试打印时,它仍然打印 2 个事件,如下所示,但只将最后 1 个事件写入新文件
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()
[输出][1][在此处输入图像描述][2]
1:<icalendar.prop.vDDDTypes object at 0x0000025CE05284F0>
2:20211011T034853Z-131@jvcweb02.wcal.nfra.io
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:20211011T034941Z-175@jvcweb02.wcal.nfra.io
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````
您正在以写入模式打开 example.ics
文件,这会覆盖现有内容(如果有)并写入新内容,这证明了为什么您只获得一个条目,我想该条目应该是循环的最后一次迭代。
一种解决方案是使用 a
或 a+
以附加模式打开文件。
另一种解决方案可能只是在循环外打开文件 - 在 for event3 in component.walk():
行之前。这样,您将在循环中保持文件打开,并写入所有需要写入的内容。然后在循环结束时关闭文件(也在循环外)。