开始使用免费开始使用

将成对的字符串解析为 datetime

到目前为止,您一直在处理 W20529 行程中预处理好的 datetime 列表。本练习中,您将回到数据清洗流程的上一步,直接处理数据最初的字符串形式。

请在 IPython shell 中查看 onebike_datetime_strings,以确定正确的格式。datetime 已为您加载。

参考
%Y4 位年份 (0000-9999)
%m2 位月份 (1-12)
%d2 位日期 (1-31)
%H2 位小时 (0-23)
%M2 位分钟 (0-59)
%S2 位秒 (0-59)

本练习是课程的一部分

在 Python 中处理日期和时间

查看课程

练习说明

  • for 循环之外,完成 fmt 字符串,填入与数据匹配的正确解析格式。
  • for 循环内,将 startend 字符串解析为 datetime 对象,并以 startend 作为键,存入 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)
编辑并运行代码