简体   繁体   English

在Python中解析ics文件。 Icalendar软件包不返回开始/结束日期和其他属性

[英]Parse ics file in Python. Icalendar package doesn't return start/end date and other properties

Im trying to parse google calendar ( cal.ics ) using icalendar package and running this script: 我正在尝试使用icalendar包解析Google日历( cal.ics )并运行以下脚本:

from icalendar import Calendar, Event
from datetime import datetime

g = open('cal.ics','rb')
gcal = Calendar.from_ical(g.read())
for component in gcal.walk():
        print component.get('summary')
        print component.get('dtstart')

g.close()

The output is correct summary but instead of start date im getting: 输出是正确的summary但不是即时start date

<icalendar.prop.vDDDTypes object at 0x10bae9b10>

Tried replacing get with decoded like this: 尝试将get替换为如下所示的decoded

    print component.decoded('summary')
    print component.decoded('dtstart')

...but got en error: ...但是出现错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-57-d6a73c485b11> in <module>()
      8 #        print component.content_line
      9         print component.name
---> 10         print component.decoded('summary')
     11         print component.get('dtstart')
     12 

~/anaconda/lib/python2.7/site-packages/icalendar/cal.pyc in decoded(self, name, default)
    238         else:
    239             if default is _marker:
--> 240                 raise KeyError(name)
    241             else:
    242                 return default

KeyError: 'summary'

All the docs i found are about adding to the calendar while im trying to parse it and eventually move parts of it into pandas. 我发现的所有文档都是关于添加日历的,而我试图对其进行解析,最终将其中的一部分移入熊猫。 If you have any ideas/suggestions please help. 如果您有任何想法/建议,请提供帮助。

I had this problem years ago- real source of frustration. 几年前,我遇到了这个问题-沮丧的真正根源。 Solution is to do 解决办法是做

component.get('dtstart').dt

In general, when you're having a problem like this, try calling dir() on it. 通常,遇到此类问题时,请尝试在其上调用dir()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM