我试图调用Google Calendar API并返回仅包含特定文本的结果。
使用以下代码段:
eventsResult = service.events().list(
calendarId='primary', timeMin=start_time, timeMax=end_time, singleEvents=True,
orderBy='startTime').execute()
events = eventsResult.get('items', [])
if not events:
print('No upcoming events found.')
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
print(start, event['summary'])
我收到以下格式的结果:
(u'2018-02-16', u'Foo')
(u'2018-02-16T10:00:00-07:00', u'Bar')
我正在尝试添加逻辑以将结果限制为包含' Foo'
的行 if not events:
print('No upcoming events found.')
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
if "Foo" in event:
print(start, event['summary'])
什么都不返回。
我是否需要将if "Foo" in event:
逻辑放在别处?我误解了用法吗?
答案 0 :(得分:0)
答案 1 :(得分:0)
事实证明,事件是一个列表。我能够进行以下更改,只输出与列表中的'foo'匹配的结果:
for event in events:
start = event['start'].get('date')
summary = event['summary']
if 'foo' in event['summary']:
print(start, summary)