Python 如何将整数转换为时间格式
我将如何将3.65这样的浮动转换为4分5秒 我试过使用:Python 如何将整数转换为时间格式,python,Python,我将如何将3.65这样的浮动转换为4分5秒 我试过使用: print(datetime.datetime.strptime('3.35','%M%-S')) 然而,我得到了回报: ValueError: '-' is a bad directive in format '%-M:%-S' 而65秒无法正确解析,因此在解析之前,您必须自己操作以首先清理数据 注意:假设seconds不是一个非常大的数字,可以使分钟数大于60 您也可以在datetime对象上使用。time()on来提取时间 pr
print(datetime.datetime.strptime('3.35','%M%-S'))
然而,我得到了回报:
ValueError: '-' is a bad directive in format '%-M:%-S'
而65秒无法正确解析,因此在解析之前,您必须自己操作以首先清理数据 注意:假设
seconds
不是一个非常大的数字,可以使分钟数大于60
您也可以在datetime对象上使用。time()
on来提取时间
print(datetime.datetime.strptime('{}.{}'.format(minute,seconds),'%M.%S').time()) #00:04:05
一个更清洁、更安全的解决办法是(考虑时间)。将所有内容转换为秒,然后再转换回小时、分钟、秒
def convert(seconds):
min, sec = divmod(seconds, 60)
hour, min = divmod(min, 60)
return "%d:%02d:%02d" % (hour, min, sec)
time='59.65'
split_time = time.split(".")
minute =int(split_time[0])
seconds = int(split_time[1])
new_seconds = minute*60 +65
datetime.datetime.strptime(convert(new_seconds),'%H:%M:%S').time()
而65秒无法正确解析,因此在解析之前,您必须自己操作以首先清理数据 注意:假设
seconds
不是一个非常大的数字,可以使分钟数大于60
您也可以在datetime对象上使用。time()
on来提取时间
print(datetime.datetime.strptime('{}.{}'.format(minute,seconds),'%M.%S').time()) #00:04:05
一个更清洁、更安全的解决办法是(考虑时间)。将所有内容转换为秒,然后再转换回小时、分钟、秒
def convert(seconds):
min, sec = divmod(seconds, 60)
hour, min = divmod(min, 60)
return "%d:%02d:%02d" % (hour, min, sec)
time='59.65'
split_time = time.split(".")
minute =int(split_time[0])
seconds = int(split_time[1])
new_seconds = minute*60 +65
datetime.datetime.strptime(convert(new_seconds),'%H:%M:%S').time()
把你的时间分成几分钟和几秒钟 如果秒数为60或更多,则添加额外的分钟数(//);二是模(%)
把你的时间分成几分钟和几秒钟 如果秒数为60或更多,则添加额外的分钟数(//);二是模(%)
首先,你应该向那些给你时间的人抱怨,数据是这样表达的 如果需要将分钟和秒作为独立值处理,则datetime对象也可能不是最佳选择 如果仍然需要将“3.65”转换为对应于“4-05”的datetime对象,则可以在将其传递给strTime()之前将其调整为有效的时间表示形式
首先,你应该向那些给你时间的人抱怨,数据是这样表达的 如果需要将分钟和秒作为独立值处理,则datetime对象也可能不是最佳选择 如果仍然需要将“3.65”转换为对应于“4-05”的datetime对象,则可以在将其传递给strTime()之前将其调整为有效的时间表示形式
看看下面的脚本,你可以知道如何让它工作几天几年,等等,这只有在我们假设格式为“hours.minutes”时才有效
看看下面的脚本,你可以知道如何让它工作几天几年,等等,这只有在我们假设格式为“hours.minutes”时才有效
3.65如何等于4分5秒?字符串中也没有
-
datetime.datetime.strtime('3.35','%M.%S')
可能会为您提供一条线索,说明您在何处进行mistake@mad3m65s==4m5s,周期令人困惑是的,lol有一个大脑屁3.65怎么等于4分5秒?字符串中也没有-
datetime.datetime.strtime('3.35','%M.%S')
可能会为您提供一条线索,说明您在何处进行mistake@mad3m65s==4m5s,周期令人困惑是的,lol有一个大脑屁你可以避免而数学循环:seconds=int(分割时间[1]);分钟偏移量,秒=divmod(秒,60);分钟=int(分割时间[0])+分钟偏移量
使用str.format
:.strtime({}.{}.format(分钟,秒)
你可以避免的循环,而
的数学循环:seconds=int(分割时间[1]);分钟偏移量,秒=divmod(秒,60);分钟=int(分割时间[0])+分钟偏移量
使用str.format
:.strtime({}.{}.format(分钟,秒)
m,s = map(int,"3.65".split("."))
m,s = (m+s)//60,s%60
dt = datetime.datetime.strptime(f"{m}-{s}","%M%-S")
import datetime
# Assuming the 3 represents the hours and the 0.65 the minutes
number = 3.65
# First, we need to split the numbero into its whole decimal part
# and its decimal part
whole_decimal_part = hours = int(number) # 3
decimal_part = number % whole_decimal_part # 0.6499999
# Now, we need to know how many extra hours are in the decimal part
extra_hours = round((decimal_part * 100) / 60) # 1
minutes = round((decimal_part * 100) % 60) # 5
hours += extra_hours # 4
time_str = "%(hours)s:%(minutes)s" % {
"hours": hours,
"minutes": minutes
} # 4:5
final_time = datetime.datetime.strptime(time_str, "%H:%M").time()
print(final_time) # 04:05:00