用 strftime() 重现 ISO 格式
在上一章,您使用 strftime() 从 date 对象创建字符串。现在您已经了解了 datetime 对象,让我们来练习做一件类似的事情。
使用 .strftime() 重新实现 .isoformat() 方法,并打印数据集中第一条行程的开始时间。
| 参考 | |
|---|---|
| %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 中处理日期和时间
练习说明
- 完成
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(____)