strptime 时间转换字符串
strptime convert string time to time
我尝试将这样的字符串时间 1:34.21
转换为时间。
我使用此 datetime.strptime('1:34.21', '%-H:%M.%S').time()
进行转换,但出现此错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/_strptime.py", line 568, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/_strptime.py", line 341, in _strptime
raise ValueError("'%s' is a bad directive in format '%s'" %
ValueError: '-' is a bad directive in format '%-H:%M.%S'
只要把'%-H:%M.%S'
改成'%H:%M.%S'
from datetime import datetime
s = '1:34.21'
print(datetime.strptime(s, '%H:%M.%S').time())
输出:
01:34:21
我尝试将这样的字符串时间 1:34.21
转换为时间。
我使用此 datetime.strptime('1:34.21', '%-H:%M.%S').time()
进行转换,但出现此错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/_strptime.py", line 568, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/_strptime.py", line 341, in _strptime
raise ValueError("'%s' is a bad directive in format '%s'" %
ValueError: '-' is a bad directive in format '%-H:%M.%S'
只要把'%-H:%M.%S'
改成'%H:%M.%S'
from datetime import datetime
s = '1:34.21'
print(datetime.strptime(s, '%H:%M.%S').time())
输出:
01:34:21