1

使用 python,我刚刚制作了两个字符串,现在想将它们转换为整数数组。

我的两个字符串是地震的开始和结束时间,看起来像这样

"00:39:59.946000"

"01:39:59.892652"

我想将这两个转换为整数数组,以便我可以使用numpy.arange()or numpy.linspace()。预期的输出应该是一个数组,该数组在开始时间和结束时间之间具有多个均匀间隔的值。例如,

array = [00:39:59.946000, 00:49:59.946000, 00:59:59.946000, 01:09:59.946000, etc...]

然后,我想将此数组的值用作图表 x 轴上的每个增量。任何建议/帮助将不胜感激。

4

4 回答 4

1
>>> [int(x) for x in eq_time if x.isdigit()]
于 2013-08-27T03:30:01.570 回答
1

您可以将时间戳转换为纪元时间吗?

于 2013-08-27T03:35:11.887 回答
0
>>> import time
>>> t1="00:39:59.946000"
>>> t2=time.strptime(t1.split('.')[0]+':2013', '%H:%M:%S:%Y') #You probably want year as well.
>>> time.mktime(t2) #Notice that the decimal parts are gone, we need to add it back
1357018799.0
>>> time.mktime(t2)+float('.'+t1.split('.')[1]) #(add ms)
1357018799.946

#put things together:
>>> def str_time_to_float(in_str):
    return time.mktime(time.strptime(in_str.split('.')[0]+':2013', '%H:%M:%S:%Y'))\
           ++float('.'+in_str.split('.')[1])
>>> str_time_to_float("01:39:59.892652")
1357022399.892652
于 2013-08-27T03:41:44.677 回答
0

由于您的字符串代表时间数据,那么看看time.strptime怎么样?

类似的东西

from datetime import datetime                                                                                                                                                                                                                                                      

t1 = datetime.strptime("2013:00:39:59.946000", "%Y:%H:%M:%S.%f")                                                                               
t2 = datetime.strptime("2013:01:39:59.892652", "%Y:%H:%M:%S.%f")
于 2013-08-27T03:43:17.977 回答