这应该很简单,但我很难接受它。基本上,我有4个字符串,我为我的for循环的每次迭代生成,然后将它们添加到列表中。我希望每4个项目成一行,我需要能够提供4列标题值。这看起来像熊猫的工作,但我不能按照我需要的方式得到它。提前感谢您的指导。以下是我到目前为止的情况:
def main():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)
deadline_list = []
pattern1 = re.compile(r'(?::)(.+$)')
pattern2 = re.compile(r'(?:.+:\s+)(\w+\s+\w+)')
StartDate = '2017-05-14'
EndDate = '2017-05-19'
print('\nGetting events within date range\n')
eventsResult = service.events().list(
calendarId='primary', timeMin=StartDate+'T00:00:01.000Z', timeMax=EndDate+'T23:59:59.000Z',
singleEvents=True,
orderBy='startTime').execute()
events = eventsResult.get('items', [])
if not events:
print('No upcoming events found.')
for event in events:
if re.search('^\d+',event['location']):
start = event['start'].get('dateTime', event['start'].get('date'))
deadline_type = pattern1.search(event['description']).group(1).rstrip('\r')
client_name = pattern2.match(event['description']).group(1)
location = ' '.join(event['location'].split())
deadline_list.extend([location, start, deadline_type, client_name])
else:
pass
print(deadline_list)
答案 0 :(得分:0)
如果您将deadline_list
传递给pandas,则需要使用:
deadline_list.append([location, start, deadline_type, client_name])
而不是.extend
。
这将创建一个列表列表,当通过
df = pd.DataFrame(deadline_list, columns=['location','start','deadline_type','client_name'])
此外,您不需要else: pass
,如果if
声明中的条件不是True
,则无论如何都不会发生任何事情。