2015-06-19 57 views
0

我試圖在python中編寫一個解析器來讀取輸入文件,然後將結果組裝成幾個數組。使用多個分隔符和標題將輸入數據讀取到陣列

的數據結構如下:

Some_numbers 
1 
5 
6 
some_vector 
[-0.99612937 -0.08789929 0.  ] 
[-0.99612937 -0.08789929 0.  ] 
[ -9.99999987e-01 1.61260621e-04 0.00000000e+00] 
Some_data 
1239 #int  
671 
471 
851 
S4RS #string 
517 
18 
48 
912 
S4RS 

到目前爲止,我已經嘗試過的方法是:

text_file = 'C:\AA\aa.txt' 
lines = open(text_file).read().splitlines() 
numbers = [] 
Vector = [] 
for line in lines: 
    line = line.strip() 
    if line.startswith('Some_numbers'): 
     continue 
     numbers.append(line) 
    if line.startswith('some_vector'): 
     continue 
     Vector.append(line) 

我也遇到過問題是: 1)擁有多個分隔符 2 )試圖根據相關部分分割數據

我也嘗試使用np.genfromtxt al與無數小時拖網互聯網。

您的意見和建議非常感謝。

回答

0

我不完全確定任何可以做到這一點的內置或庫函數,但是您的for循環中有一些明顯的問題。

首先,continue之後的一個聲明if塊內 - numbers.append(line)(或等效向量)。該語句永遠不會執行,因爲繼續會將控制權發送回for循環的開始處,counter變量會遞增。

其次,你不是根據sections閱讀,這是你的輸入內容,但我會說你幾乎沒有閱讀任何東西。

一個示例代碼,將工作(數字和載體是) -

text_file = 'C:\AA\aa.txt' 
lines = open(text_file).read().splitlines() 
numbers = [] 
Vector = [] 
section = '' 
for line in lines: 
    line = line.strip() 
    if line.startswith('Some_numbers'): 
     section = 'numbers' 
     continue 
    elif line.startswith('some_vector'): 
     section = 'vectors' 
     continue 
    elif section == 'numbers': 
     numbers.append(line) # or numbers.append(int(line)) , whichever you want 
    elif section == 'vectors': 
     Vector.append(line) 

請注意,上面的代碼只對數字和載體,其它部分都需要由您來編碼。

+0

謝謝阿南德,現在工作得很好。 – Amateur

+0

很高興爲你效勞。 –

相關問題