>> datetime.datetime.strptime(a, "%Y-%m-%d %H:%M:%f") Traceback -6ren">
gpt4 book ai didi

python - 字符串中的日期时间对象, float 中的秒数

转载 作者:太空宇宙 更新时间:2023-11-03 12:27:12 24 4
gpt4 key购买 nike

>>> a = "2016-03-22 12:33:45.7565"
>>> datetime.datetime.strptime(a, "%Y-%m-%d %H:%M:%f")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 328, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: .7565
>>> datetime.datetime.strptime(a, "%Y-%m-%d %H:%M:%S")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 328, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: .7565

我已经使用了 %S%f 但如果是类型 float 我如何处理秒数?

任何帮助将不胜感激

最佳答案

小数点后的数字为微秒,与秒分开格式化:

>>> a = "2016-03-22 12:33:45.7565"
>>> datetime.datetime.strptime(a, "%Y-%m-%d %H:%M:%S.%f")
datetime.datetime(2016, 3, 22, 12, 33, 45, 756500)

关于python - 字符串中的日期时间对象, float 中的秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37064606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com