2014-01-10 73 views
1

這一直在殺死我整天/晚,我似乎無法想出一個解決方案。基本上,我有一個文本文件,其中包含一個二維矢量(由C++程序生成)在雙打中。我需要在Python中將它讀入二維數組,以便繪製譜圖。這裏是數據的樣子:從文本文件讀取塊到二維數組

-18.2258 -18.3581 -18.7323 -19.2183 -19.8016 -20.6132 -21.8101 -22.5386 -21.8071  
-20.9063 -20.4136 -20.3022 -20.3428 -20.4091 -20.6703 -21.0293 -21.5167 -22.1915  
-23.0438 -23.9086 -24.5955 -26.2508 -26.0188 -22.2163 -19.933 -18.6816 -18.1048 
-18.0222 18.3233 -19.0456 -20.3134 -22.7954 -25.8716 -21.4845 -19.1923 -17.9268 
-17.4657 -17.3888 -16.9999 -16.4006 -15.9175 -15.8319 -16.1705 -16.6967 -17.0734 


-7.92685 -10.8266 -16.392 -12.4901 -13.0831 -17.7215 -17.5159 -14.1485 -12.9897 -12.0444 
-11.8363 -12.6952 -12.9652 -14.3788 -13.8465 -17.529 -17.4747 -11.9521 -12.545 -13.8976 
-12.4176 -15.3273 -14.8081 -19.4117 -17.9596 -16.2607 -16.7505 -15.8918 -16.5602 
-17.2225 -16.9048 -15.1381 -17.37 -16.43 -14.9437 -14.9821 

每個數據塊在文本文件中用2行分隔。

我曾嘗試以下:

with open('spec.txt') as file: 
    array2d = [[float(digit) for digit in line.split()] for line in file] 

但是,這並不工作,我只是似乎得到了很多產生的陣列。

任何人有任何想法來解決這個問題?

P.S.每個塊的大小相同。但是,爲了縮短這個問題,我只包含了一個示例。

+0

你用什麼來繪圖? 「2d數組」究竟是什麼意思?列表清單?或者是一個'numpy'數組? – hpaulj

+0

@hpaulj嘿,我使用matplotlib來繪製..我試過了Ashwini Chaudhary的實現,但是,它不起作用。結果有點搞砸 – Phorce

+0

如果你的第二個塊的數字與第一個塊的數量相同,那麼測試起來會更容易。這樣,列表的結果列表可以直接輸入到'numpy.array'來創建一個2d數組。 – hpaulj

回答

2
raw_text = """-18.2258 -18.3581 -18.7323 -19.2183 -19.8016 -20.6132 -21.8101 -22.5386 -21.8071  
-20.9063 -20.4136 -20.3022 -20.3428 -20.4091 -20.6703 -21.0293 -21.5167 -22.1915  
-23.0438 -23.9086 -24.5955 -26.2508 -26.0188 -22.2163 -19.933 -18.6816 -18.1048 
-18.0222 18.3233 -19.0456 -20.3134 -22.7954 -25.8716 -21.4845 -19.1923 -17.9268 
-17.4657 -17.3888 -16.9999 -16.4006 -15.9175 -15.8319 -16.1705 -16.6967 -17.0734 


-7.92685 -10.8266 -16.392 -12.4901 -13.0831 -17.7215 -17.5159 -14.1485 -12.9897 -12.0444 
-11.8363 -12.6952 -12.9652 -14.3788 -13.8465 -17.529 -17.4747 -11.9521 -12.545 -13.8976 
-12.4176 -15.3273 -14.8081 -19.4117 -17.9596 -16.2607 -16.7505 -15.8918 -16.5602 
-17.2225 -16.9048 -15.1381 -17.37 -16.43 -14.9437 -14.9821""" 
#in your example raw_text = open(some_file).read() 
blocks = raw_text.split("\n\n\n") 
split_blicks = [[float(v) for v in block.split()] for block in blocks] 

是你想要的嗎?

+0

是的,但是,它是否必須進行硬編碼? – Phorce

+0

什麼?沒有看到註釋行 –

0

斯普利特在空行數據:

def split_at_empty_lines(filename): 
    with open(filename) as f: 
     arr = [] 
     for line in f: 
      #If the line is empty and arr is not empty, means it's 
      #time to return the collected items and set `arr` back to []. 
      if not line.strip() and arr: 
       yield arr 
       arr = [] 
      #If the line is not empty then simply collect the items in `arr` 
      elif line.strip(): 
       arr.extend(float(x) for x in line.split()) 
      #Ignore the case of empty line and empty `arr` 

     #Check if arr is not empty or not, if not empty returns its content. 
     if arr: yield arr 
...   
>>> list(split_at_empty_lines('abc1.txt')) 
[ 
[-18.2258, -18.3581, -18.7323, -19.2183, -19.8016, -20.6132, -21.8101, -22.5386, -21.8071, -20.9063, -20.4136, -20.3022, -20.3428, -20.4091, -20.6703, -21.0293, -21.5167, -22.1915, -23.0438, -23.9086, -24.5955, -26.2508, -26.0188, -22.2163, -19.933, -18.6816, -18.1048, -18.0222, 18.3233, -19.0456, -20.3134, -22.7954, -25.8716, -21.4845, -19.1923, -17.9268, -17.4657, -17.3888, -16.9999, -16.4006, -15.9175, -15.8319, -16.1705, -16.6967, -17.0734], 
[-7.92685, -10.8266, -16.392, -12.4901, -13.0831, -17.7215, -17.5159, -14.1485, -12.9897, -12.0444, -11.8363, -12.6952, -12.9652, -14.3788, -13.8465, -17.529, -17.4747, -11.9521, -12.545, -13.8976, -12.4176, -15.3273, -14.8081, -19.4117, -17.9596, -16.2607, -16.7505, -15.8918, -16.5602, -17.2225, -16.9048, -15.1381, -17.37, -16.43, -14.9437, -14.9821] 
] 
+0

謝謝!但是,我怎麼能將這個方法的結果存儲在另一個數組中,以便它可以被繪製? – Phorce

+0

@ user1326876我不明白,簡單地調用它的list()(如代碼所示),你會得到一個列表清單。 –

0

你可以頗有幾分與列表內涵酌減。

with open('myfile') as f: 
    return ([float(x) for x in l.split() if l] for l in (raw.strip() for raw in f)) 

請注意,外部parens使得此返回一個生成器,而不是在返回任何內容之前處理整個文件。

相關問題