所以我試圖從文本文件中提取一些數據。目前我能得到包含數據,這又使我的輸出看起來像這樣的正確的路線:將字符串列表轉換爲Numpy數組(Python)
[ 0.2 0.148 100. ]
[ 0.3 0.222 100. ]
[ 0.4 0.296 100. ]
[ 0.5 0.37 100. ]
[ 0.6 0.444 100. ]
所以基本上我有5名名單在每一個字符串。然而,正如你可以想象的,我想把所有這些都分解成一個numpy數組,每個字符串分成3個值。就像這樣:
[[0.2, 0.148, 100],
[0.3, 0.222, 100],
[0.4, 0.296, 100],
[0.5, 0.37, 100],
[0.6, 0.444, 100]]
但是由於是在輸出的分離器是隨機的,即我不知道這是否是3位,5個空格或製表符,我是那種在如何做到這一點失去了。
UPDATE:
所以數據看起來有點像這樣:
data_file =
Equiv. Sphere Diam. [cm]: 6.9
Conformity Index: N/A
Gradient Measure [cm]: N/A
Relative dose [%] Dose [Gy] Ratio of Total Structure Volume [%]
0 0 100
0.1 0.074 100
0.2 0.148 100
0.3 0.222 100
0.4 0.296 100
0.5 0.37 100
0.6 0.444 100
0.7 0.518 100
0.8 0.592 100
Uncertainty plan: U1 X:+3.00cm (variation of plan: CT1)
Dose Cover.[%]: 100.0
Sampling Cover.[%]: 100.0
Relative dose [%] Dose [Gy] Ratio of Total Structure Volume [%]
0 0 100
0.1 0.074 100
0.2 0.148 100
0.3 0.222 100
0.4 0.296 100
0.5 0.37 100
0.6 0.444 100
和代碼來獲得線是:
with open(data_file) as input_data:
# Skips text before the beginning of the interesting block:
for line in input_data:
if line.strip() == 'Relative dose [%] Dose [Gy] Ratio of Total Structure Volume [%]': # Or whatever test is needed
break
# Reads text until the end of the block:
for line in input_data: # This keeps reading the file
if line.strip() == 'Uncertainty plan: U1 X:+3.00cm (variation of plan: CT1)':
break
text_line = np.fromstring(line, sep='\t')
print text_line
所以數據也自之前的文本是隨機的,所以我不能只說「跳過前5行」,但是標題總是相同的,並且它也是一樣的(在下一個數據開始之前)。所以我只需要一種方法來獲得原始數據,將其放入一個數組中,然後我可以從那裏使用它。
希望它現在更有意義。
使用正則表達式來分割'\ s +' – BlackBear
輸入在缺少引號的情況下應該是字符串嗎? – languitar
它沒有引號,這是肯定的。如果不是字符串,那麼正確的術語是什麼? –