2011-11-28 48 views

回答

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() 
1

为codespeak包中的文件没有提到删除功能,因此除非有一个未公开的一个就地上的现有文件的工作,你需要阅读日历文件,列举了成分:

for k,v in cal.items(): 

和写入之外的所有那些相同的部件被删除到一个新的文件。

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

+0

cal.items是一个空列表。不是特别有用。 – GuruYaya