格式化后保留未转换的数据
Unconverted Data Remains after Format
我已经查看了与 strptime 和未转换数据残留相关的其他问题,但很难看到我在代码中出错的地方。
currentTime = format(currentTime, '%H:%M:%S')
#remove the current time from phone time
timeDifferenceValue = datetime.datetime.strptime(str(currentTime), FMS) - datetime.datetime.strptime(
str(ptime) + ":00", FMS)
else:
time_left = time_left[-7:]
time_leftHatch = datetime.datetime.strptime(time_left, FMS) - timeDifferenceValue
time_leftHatch = format(time_leftHatch, '%H:%M:%S')
在此阶段发现错误:
time_leftHatch = datetime.datetime.strptime(time_left, FMS) - timeDifferenceValue
timeleft和timeDifference Value的值为:
time_left = 1:29:47
时间差值 = 0:13:31
错误:
未转换的数据仍然存在::47
John 在评论中提到我应该更改删除不必要的 strptime 的过度使用。这似乎解决了未转换的数据残留问题。
比如timeDifferenceValue已经是时间格式了,根本不需要改
有什么想法吗?
眼前的问题是:
datetime.datetime.strptime("0:"+str(time_left), FMS)
我不确定你为什么要在前面加上“0:”,但这会导致“0:1:29:47”,你的 strptime 格式只解析前三个值。 "unconverted data" 错误让您知道您的输入字符串中有一些格式字符串未处理的额外信息。
不过,更好的解决方法是停止字符串化和 strptiming,这将继续让您感到悲伤。您已经有了一些在代码中使用 timedelta
的提示。将 Python returns 中的时间之间的差异作为一个时间增量,并将时间增量添加到时间 returns 一个新时间。这样做可以让您获得结构化数据和明确定义的结果。
例如,
# do this early, don't go back to string
phone_time = datetime.datetime.strptime(ptime + ':00', FMS)
# and leave this a datetime instead of stringifying it later
currentTime = datetime.datetime.now() + timedelta(hours=1)
# now you can just take the difference
timeDifferenceValue = currentTime - ptime
并且:
if str(timeDifferenceValue).__contains__("-"):
变成
if timeDifferenceValue < timedelta(0):
希望能为您指明正确的方向!
我已经查看了与 strptime 和未转换数据残留相关的其他问题,但很难看到我在代码中出错的地方。
currentTime = format(currentTime, '%H:%M:%S')
#remove the current time from phone time
timeDifferenceValue = datetime.datetime.strptime(str(currentTime), FMS) - datetime.datetime.strptime(
str(ptime) + ":00", FMS)
else:
time_left = time_left[-7:]
time_leftHatch = datetime.datetime.strptime(time_left, FMS) - timeDifferenceValue
time_leftHatch = format(time_leftHatch, '%H:%M:%S')
在此阶段发现错误:
time_leftHatch = datetime.datetime.strptime(time_left, FMS) - timeDifferenceValue
timeleft和timeDifference Value的值为:
time_left = 1:29:47 时间差值 = 0:13:31
错误: 未转换的数据仍然存在::47
John 在评论中提到我应该更改删除不必要的 strptime 的过度使用。这似乎解决了未转换的数据残留问题。
比如timeDifferenceValue已经是时间格式了,根本不需要改
有什么想法吗?
眼前的问题是:
datetime.datetime.strptime("0:"+str(time_left), FMS)
我不确定你为什么要在前面加上“0:”,但这会导致“0:1:29:47”,你的 strptime 格式只解析前三个值。 "unconverted data" 错误让您知道您的输入字符串中有一些格式字符串未处理的额外信息。
不过,更好的解决方法是停止字符串化和 strptiming,这将继续让您感到悲伤。您已经有了一些在代码中使用 timedelta
的提示。将 Python returns 中的时间之间的差异作为一个时间增量,并将时间增量添加到时间 returns 一个新时间。这样做可以让您获得结构化数据和明确定义的结果。
例如,
# do this early, don't go back to string
phone_time = datetime.datetime.strptime(ptime + ':00', FMS)
# and leave this a datetime instead of stringifying it later
currentTime = datetime.datetime.now() + timedelta(hours=1)
# now you can just take the difference
timeDifferenceValue = currentTime - ptime
并且:
if str(timeDifferenceValue).__contains__("-"):
变成
if timeDifferenceValue < timedelta(0):
希望能为您指明正确的方向!