Ical in python - 删除事件

时间:2011-11-28 11:26:02

标签: python icalendar

我正在使用python的iCalendar包,你可以在这里看到: http://codespeak.net/icalendar/

我想删除一个活动。谁能想到办法呢?

由于

2 个答案:

答案 0 :(得分:1)

codespeak软件包的文档没有提及删除功能,所以除非有一个在现有文件上就地工作的未记录文件,否则你需要阅读一个日历文件,枚举其组件:

for k,v in cal.items():

并将除要删除的组件之外的所有组件写入新文件。

由于RFC5545是一个简单的规范,因此直接从cal字典中删除记录可能是安全的。但总的来说,组件或副作用之间可能存在依赖关系(例如更新索引或校验和),因此直接操作结构而不是调用方法可能会产生内部不一致。由于这是Python,您可以检查副作用的add()方法并创建自己的delete()方法,以反转它所做的一切。

答案 1 :(得分:0)

如果有人需要,这是在python中使用icalendar库(http://codespeak.net/icalendar/)删除组件的功能:

def del_component_with_uid(cal,uid):
    cal_str=cal.to_ical()
    component_find=find_component_with_uid(cal,uid)
    if component_find is not False:
        firstfind = unicode(cal_str, encoding="latin1").find(unicode(component_find, encoding="latin1"))
        export_cal(cal_str[:firstfind]+cal_str[firstfind+len(component_find):])

def find_component_with_uid(cal,uid):
    propert = uid
    for component in cal.subcomponents:
        for prop in component.property_items():
            if prop[0] == 'UID' and prop[1] == uid:
                return component.to_ical()
    return False

def export_cal(calendar_str):
    f = open('./Calendars/example.ics', 'w')
    f.write(calendar_str)
    f.close()