在Python中搜索iCal格式文件中的事件

时间:2015-01-28 14:39:38

标签: python datetime

我想只查看今天在iCal文件中应用的事件。这是我的代码:

from icalendar import Calendar, Event
from datetime import datetime

g = open('reachcalendar.ics','rb')
cal = Calendar.from_ical(g.read())

for component in cal.walk():
  if component.name == "VEVENT":
    if component.get('dtstart').date() == datetime.today().date()
      print component.get('summary')
      print component.get('dtstart').dt

g.close()

但这会导致语法无效:

  File "today.py", line 9
    if component.get('dtstart').date() == datetime.today().date()
                                                                   ^
SyntaxError: invalid syntax

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您错过了该行末尾的:

if component.get('dtstart').date() == datetime.today().date():
#                                                        HERE^

除此之外,你可能不应该在循环中调用datetime.today(),之前调用它,记住值并在循环中使用:

today = datetime.today().date()
for component in cal.walk():
    if component.name == "VEVENT" and component.get('dtstart').date() == today:
        print component.get('summary')
        print component.get('dtstart').dt