【问题标题】:Parsing iCal feed with multiple events with Python and vobject使用 Python 和 vobject 解析具有多个事件的 iCal 提要
【发布时间】:2011-10-06 02:25:00
【问题描述】:

我正在尝试解析包含多个事件的提要,但它只返回一个项目

ics = urllib.urlopen("https://www.google.com/calendar/ical/pcolalug%40gmail.com/public/basic.ics").read()
events = []

components = vobject.readComponents(ics)
for event in components:
    to_zone = tz.gettz('America/Chicago')

    date = event.vevent.dtstart.value.astimezone(to_zone)
    description = event.vevent.description.value

    events.append({
                'start': date.strftime(DATE_FORMAT),
                'description': description if description else 'No Description', 
                })

return {'events': events[:10]}

我做错了什么?

【问题讨论】:

    标签: python icalendar vobject


    【解决方案1】:

    改用 icalendar 代替 vobject,效果好多了。

    ics = urllib.urlopen("https://www.google.com/calendar/ical/pcolalug%40gmail.com/public/basic.ics").read()
    events = []
    
    cal = Calendar.from_string(ics)
    
    for event in cal.walk('vevent'):
        to_zone = tz.gettz('America/Chicago')
    
        date = event.get('dtstart').dt.astimezone(to_zone)
        description = event.get('description')
    
        events.append({
                    'start': date.strftime(DATE_FORMAT),
                    'description': description if description else 'No Description', 
                    })
    
    return {'events': events[:10]}
    

    【讨论】:

    • icalendar 在 linux 中不能作为包使用。 vobject 非常有可能。
    • 那不是真的,你只需“pip install icalendar”就可以了。它是纯 python 并且没有 C 扩展,因此它适用于所有平台。 pypi.python.org/pypi/icalendar/3.11.4
    猜你喜欢
    • 2021-10-08
    • 1970-01-01
    • 2016-01-24
    • 2012-08-14
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多