我有这个字符串
14 Mai 2014
我想把它转换成iso 8601
我读了this answer和 this one ,
首先我尝试将字符串转换为日期,然后将其转换为 iso 格式:
test_date = datetime.strptime("14 Mai 2014", '%d %m %Y')
iso_date = test_date.isoformat()
我遇到了这个错误:
ValueError: time data '14 Mai 2014' does not match format '%d %m %Y'
请您参考如下方法:
根据 Python strftime reference %m 表示一个月中的第几天,在您的情况下,“Mai”似乎是您当前语言环境中的月份名称,您必须使用此 %b 格式。所以你的代码应该是这样的:
test_date = datetime.strptime("14 Mai 2014", '%d %b %Y')
iso_date = test_date.isoformat()
不要忘记设置语言环境。
它适用于英语语言环境:
>>> from datetime import datetime
>>> test_date = datetime.strptime("14 May 2014", '%d %b %Y')
>>> print(test_date.isoformat())
2014-05-14T00:00:00
