用 strftime() 重現 ISO 格式
在上一章中,你用 strftime() 從 date 物件建立字串。現在你已經認識 datetime 物件了,我們來練習做類似的事。
用 .strftime() 來重現 .isoformat() 方法,並列印資料集中第一筆行程的起始時間。
| Reference | |
|---|---|
| %Y | 4 digit year (0000-9999) |
| %m | 2 digit month (1-12) |
| %d | 2 digit day (1-31) |
| %H | 2 digit hour (0-23) |
| %M | 2 digit minute (0-59) |
| %S | 2 digit second (0-59) |
本練習屬於課程
在 Python 處理日期與時間
練習說明
- 完成
fmt,使其符合 ISO 8601 的格式。 - 分別用
.isoformat()和.strftime()列印first_start;兩者應該要一致。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import datetime
from datetime import datetime
# Pull out the start of the first trip
first_start = onebike_datetimes[0]['start']
# Format to feed to strftime()
fmt = "____"
# Print out date with .isoformat(), then with .strftime() to compare
print(first_start.isoformat())
print(____)