我有大量的 timestamp
字符串,例如 2017-04-18 05:03:40.966084
并且脚本需要向 timestamp< 添加秒数
。我一直在尝试将字符串转换为合法的 timestamp
对象,然后使用 timedelta
向其添加秒数,但由于某种原因它无法正常工作。
这是我目前所拥有的:
duration = "150.100"
timestamp = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S.%f")
timestamp_e = timestamp+timedelta(seconds=duration)
我这样做的方式正确吗?
最佳答案
是的 timedelta
是调整时间戳的好方法。但是请注意,您的 duration
变量是一个字符串。根据 documentation它应该是一个整数或 float :
All arguments are optional and default to 0. Arguments may be integers or floats, and may be positive or negative.
关于python - 在python中为时间戳添加秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47324442/