Skip to main content
 首页 » 编程设计

Python icalender 在 ORGANIZER 中因变音符号而窒息

2025年05月04日7langtianya

我正在尝试修复我从 horde 获得的 ical 文件(重复事件在导入后两小时内关闭)。所以我写了下面的脚本,它在 ical 文件的“ORGANIZER”标签中被变音符号阻塞:

#!/usr/bin/python 
 
import sys 
from icalendar import Calendar, Event 
from datetime import timedelta 
 
# reccurring events are off by two hours 
reccurrence_timedelta=timedelta(hours=2) 
 
# default 
ical_file="test.ical" 
 
# input file  
try: 
    ical_file=sys.argv[1] 
except IndexError: 
    pass 
 
cal = Calendar.from_ical(open(ical_file,'rb').read()) 
for component in cal.walk(): 
    if component.name == "VEVENT": 
        try: 
            component['rrule'] 
            dtstart = component.decoded('dtstart') 
            dtend = component.decoded('dtend') 
            new_dtstart=dtstart + reccurrence_timedelta 
            new_dtend =dtend + reccurrence_timedelta 
            new_cal=Calendar() 
            new_cal.add('dtstart',new_dtstart) 
            new_cal.add('dtend',new_dtend) 
            component['dtstart']=new_cal['dtstart'] 
            component['dtend']=new_cal['dtend'] 
        except KeyError: 
            pass 
 
new_ics_filename = "corrected_" + ical_file 
newics_file = open(new_ics_filename, 'wb') 
cal_as_ical=cal.to_ical() 
newics_file.write(cal_as_ical) 
newics_file.close() 

以utf-8编码的ical文件是

BEGIN:VCALENDAR 
VERSION:2.0 
X-WR-CALNAME:Kalender von acme\, admin 
PRODID:-//The Horde Project//Horde_iCalendar Library\, Horde 3.3.5//EN 
METHOD:PUBLISH 
BEGIN:VEVENT 
DTSTART:20130416T100000Z 
DTEND:20130416T110000Z 
DTSTAMP:20130416T092616Z 
UID:20130416112341.10064jz0k4j7uem8@acmenet.de 
CREATED:20130416T092341Z 
LAST-MODIFIED:20130416T092341Z 
SUMMARY:wichtiger termin 1 
ORGANIZER;CN="acme, ädmin":mailto:adm-acme@mydomain.de 
LOCATION:im büro 
CLASS:PUBLIC 
STATUS:CONFIRMED 
TRANSP:OPAQUE 
END:VEVENT 
END:VCALENDAR 

错误是

No handlers could be found for logger "icalendar" 
Traceback (most recent call last): 
   File "./fix_horde_ical.py", line 48, in <module> 
    cal_as_ical=cal.to_ical() 
  File "/usr/local/lib/python2.7/dist-packages/icalendar-3.3-py2.7.egg/icalendar    /cal.py", line 485, in to_ical 
    return self.content_lines().to_ical() 
  File "/usr/local/lib/python2.7/dist-packages/icalendar-3.3-py2.7.egg/icalendar/cal.py", line 480, in content_lines 
    contentlines.append(Contentline.from_parts((name, params, values))) 
  File "/usr/local/lib/python2.7/dist-packages/icalendar-3.3-py2.7.egg/icalendar  /parser.py", line 510, in from_parts 
    % (name, params, values)) 
ValueError: Property: 'ORGANIZER' Wrong values "Parameters({'CN': 'acme, \xc3\xa4dmin'})" or "'mailto:adm-acme@mydomain.de'" 

“LOCATION”标签的元音变音好像没有问题,但是“ORGANIZER”的元音变音有问题。

从错误中可以看出,我使用的是python 2.7和icalendar 3.3。我是否需要遍历每个 ORGANIZER 并删除非 ascii 字符?或者是否有更通用、更简单的方法?

请您参考如下方法:

认为我修复了它:

--- parser.py.orig  2013-04-17 21:29:47.253235481 +0200 
+++ parser.py   2013-04-17 22:04:44.323634292 +0200 
@@ -296,7 +296,7 @@ 
     items = self.items() 
     items.sort()  # To make doctests work 
     for key, value in items: 
-            value = paramVal(value) 
+            value = paramVal(unicode(value, DEFAULT_ENCODING)) 
         result.append('%s=%s' % (key.upper(), 
                                  value.encode(DEFAULT_ENCODING))) 
     return ';'.join(result) 

另见github上的issue: https://github.com/collective/icalendar/issues/101?source=c