python将字符串转换为整数数组

时间:2013-08-27 03:19:12

标签: python arrays numpy string-conversion

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

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

"00:39:59.946000"

"01:39:59.892652"

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

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

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

4 个答案:

答案 0 :(得分:1)

>>> [int(x) for x in eq_time if x.isdigit()]

答案 1 :(得分:1)

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

答案 2 :(得分: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

答案 3 :(得分: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")
相关问题