將成對字串解析為 datetime
到目前為止,你一直在使用事先處理好的 W20529 行程 datetime 清單。這一題要往資料清理流程再往前一步,直接處理原始的字串。
請在 IPython shell 中查看 onebike_datetime_strings,找出正確的格式。datetime 已經替你載入。
| Reference | |
|---|---|
| %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字串解析後,放入trip字典中,鍵為start與end,值為對應的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)