所以我试图从文本文件中提取一些数据。目前我能得到包含数据,这又使我的输出看起来像这样的正确的路线:将字符串列表转换为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
它没有引号,这是肯定的。如果不是字符串,那么正确的术语是什么? –