par*_*rik 2 python datetime iso8601
我有这个字符串
14 Mai 2014
Run Code Online (Sandbox Code Playgroud)
我想将其转换为iso 8601
首先我尝试将字符串转换为日期,然后将其转换为 iso 格式:
test_date = datetime.strptime("14 Mai 2014", '%d %m %Y')
iso_date = test_date.isoformat()
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
ValueError: time data '14 Mai 2014' does not match format '%d %m %Y'
Run Code Online (Sandbox Code Playgroud)
根据Python strftime 参考 %m意味着一个月中的某一天,在你的情况下,“Mai”似乎是你当前语言环境中的月份名称,你必须使用这种%b格式。所以你的代码应该是这样的:
test_date = datetime.strptime("14 Mai 2014", '%d %b %Y')
iso_date = test_date.isoformat()
Run Code Online (Sandbox Code Playgroud)
并且不要忘记设置区域设置。
对于英语语言环境,它有效:
>>> from datetime import datetime
>>> test_date = datetime.strptime("14 May 2014", '%d %b %Y')
>>> print(test_date.isoformat())
2014-05-14T00:00:00
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5926 次 |
| 最近记录: |