開始使用免費開始

將成對字串解析為 datetime

到目前為止,你一直在使用事先處理好的 W20529 行程 datetime 清單。這一題要往資料清理流程再往前一步,直接處理原始的字串。

請在 IPython shell 中查看 onebike_datetime_strings,找出正確的格式。datetime 已經替你載入。

Reference
%Y4 位數年份(0000-9999)
%m2 位數月份(1-12)
%d2 位數日期(1-31)
%H2 位數小時(0-23)
%M2 位數分鐘(0-59)
%S2 位數秒(0-59)

本練習屬於課程

在 Python 處理日期與時間

檢視課程

練習說明

  • for 迴圈外,將 fmt 字串填入正確的資料解析格式。
  • for 迴圈內,將 startend 字串解析後,放入 trip 字典中,鍵為 startend,值為對應的 datetime 物件。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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)
編輯並執行程式碼