開始使用免費開始

用 strftime() 重現 ISO 格式

在上一章中,你用 strftime()date 物件建立字串。現在你已經認識 datetime 物件了,我們來練習做類似的事。

.strftime() 來重現 .isoformat() 方法,並列印資料集中第一筆行程的起始時間。

Reference
%Y4 digit year (0000-9999)
%m2 digit month (1-12)
%d2 digit day (1-31)
%H2 digit hour (0-23)
%M2 digit minute (0-59)
%S2 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(____)
編輯並執行程式碼