我有一个result.dat文件,其中包含以下数据:
7522126 0 0 0 0 0 0 -419.795 -186.24 1852.86 0.134695 -0.995462 -2.53153
7825452 0 0 0 0 0 0 -419.795 -186.24 1852.86 0.134695 -0.995462 -2.53153
8073799 0 0 0 0 0 0 -345.551 -140.711 1819.04 -0.0220266 -0.85992 -2.29598
这些值分别由制表符分隔。
我想在每行的第8列中提取值,然后将其保存到数组中。所以输出应该是这样的:
-419.795
-419.795
-345.551
最简单的方法是什么?
with open('results.dat') as f:
[line.split()[7] for line in f]
或定义一个功能,
get_col = lambda col: (line.split('\t')[col-1] for line in open('results.dat'))
现在使用所需的列号调用该函数。get_col(8)
给出第八列数据。要将其存储在数组中,
array.array('d',map(float,get_col(8)))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句