将成对的字符串解析为 datetime
到目前为止,您一直在处理 W20529 行程中预处理好的 datetime 列表。本练习中,您将回到数据清洗流程的上一步,直接处理数据最初的字符串形式。
请在 IPython shell 中查看 onebike_datetime_strings,以确定正确的格式。datetime 已为您加载。
| 参考 | |
|---|---|
| %Y | 4 位年份 (0000-9999) |
| %m | 2 位月份 (1-12) |
| %d | 2 位日期 (1-31) |
| %H | 2 位小时 (0-23) |
| %M | 2 位分钟 (0-59) |
| %S | 2 位秒 (0-59) |
本练习是课程的一部分
在 Python 中处理日期和时间
练习说明
- 在
for循环之外,完成fmt字符串,填入与数据匹配的正确解析格式。 - 在
for循环内,将start和end字符串解析为datetime对象,并以start和end作为键,存入trip字典。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Write down the format string
fmt = "____"
# Initialize a list for holding the pairs of datetime objects
onebike_datetimes = []
# Loop over all trips
for (start, end) in onebike_datetime_strings:
trip = {'start': datetime.____(____, ____),
'end': datetime.____(____, ____)}
# Append the trip
onebike_datetimes.append(trip)