如何在Python中将HH:MM:SS.SSS格式的时间转换为十进制数?示例:我想将时间“ 00:00:26.94”转换为十进制数字,以便可以将十进制数字用作绘制图表的数据。
答案 0 :(得分:1)
您可以将plt.plot_date()
与mdates.date2num()
结合使用。
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
d = dt.datetime.strptime("00:00:26.94","%H:%M:%S.%f")
plt.plot_date(mdates.date2num(d),1)
plt.gca().set_xlim((dt.datetime.strptime("00:00:25","%H:%M:%S"),
dt.datetime.strptime("00:00:27","%H:%M:%S")))
plt.show()
答案 1 :(得分:0)
time = "21:14:05.00" # Hours, minutes, seconds and hundreds of seconds
(h,m,s) = time.split(':') # Get hours, minutes and seconds
hours = float(h)
minutes = float(m)
# Divide the seconds from the hundreds of seconds, and assign in a []
a = s.split('.')
seconds = float(a[0])
hundreds = float(a[1])
decimalt = hours*3600.0 + minutes*60.0 + seconds + hundreds/100.0
print(time)
print (decimalt)