我有一个文本文件(由fortran程序创建),格式如下:
-12.7414170939064 1
-11.5491412174559 2
-9.71491476113225 3
-7.58225748434563 4
-7.31861308884334 5
-6.34960810316479 6
-4.80519553745030 7
-3.90146854139010 8
-3.24840468213061 9
-3.00329610180486 10
每行以几个空格开头。然后浮动。接下来是一些空格,然后是整数。我想将每行的第一个浮点数存储到一个浮点数组中,而不是其他任何内容。第二列中的整数无关紧要。我如何在python中执行此操作?
答案 0 :(得分:1)
file = open('test.txt', 'r') #open file for reading
list_with_floats = []
for line in file:
temp = line.split() #create a list with the float and the int as list elements
list_with_floats.append(temp[0]) #store the float in the list
print(list_with_floats)
['-12.7414170939064', '-11.5491412174559', '-9.71491476113225', '-7.58225748434563', '-7.31861308884334', '-6.34960810316479', '-4.80519553745030', '-3.90146854139010', '-3.24840468213061', '-3.00329610180486']