【发布时间】:2025-12-29 23:10:07
【问题描述】:
我想将两个字符串列表转换为具有相同时间格式的列表。
这是我们的两个列表:
# first list: strings with UTC format
firstlist = ['2013-08-16T07:35:01Z','2012-11-17T17:07:49Z','2012-11-09T23:24:13Z']
# second list: strings with day-month-year format
secondlist = ['04-06-2016','31-10-2018','12-04-2019']
我想转换这两个列表并为每个项目获取相同的格式year-month-day:
['2013-08-16','2012-11-17','2012-11-09'] # expected for the first list
['2016-06-04','2018-10-31','2019-04-12'] # expected for the second list
我尝试每个列表只使用一项:
import time
time.strptime("2013-08-16T07:35:01Z", "%d %m %y")
time.strptime("04-06-2016", "%d %m %y")
但我得到一个错误:
ValueError: time data '2013-08-16T07:35:01Z' does not match format '%d %m %y'
但我仍然很困惑。我的方法可能有问题,但我很难找到适合这种情况的方法。
【问题讨论】:
标签: python python-3.x datetime time