2013-08-27 25 views
1

使用python,我剛做了兩個字符串,現在想將它們轉換爲整數數組。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軸上的每個增量。任何意見/援助將不勝感激。

+0

什麼是預期的輸出線的東西嗎? – inspectorG4dget

+0

上面的預期輸出應該是一個數組,其中開始和結束時間之間的數值具有均勻間隔的數值。 例如,array = [00:39:59.946000,00:49:59.946000,00:59:59.946000,01:09:59.946000等] 我想使用這個數組的值作爲每個增量在我的圖的x軸上。 –

+0

請更新您的問題,而不是評論。 – Droogans

回答

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

你的意思是'isdigit()'? –

1

只能將時間戳轉換爲紀元時間嗎?

+0

這可能是正確的做法。 – Droogans

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 
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") 
+0

小數點後的部分全部沒了... –

+0

試試看datetime的版本。據此編輯。 – jrs