我有一个由制表符分隔的文本文件(data.txt),如下所示:
name height weight
A 15.5 55.7
B 18.9 51.6
C 17.4 67.3
D 11.4 34.5
E 23.4 92.1
下面的程序以字符串列表的形式给出结果。
with open('data.txt', 'r') as f:
col1 = [line.split()[0] for line in f]
data1 = col1 [1:]
print (data1)
with open('data.txt', 'r') as f:
col2 = [line.split()[1] for line in f]
data2 = col2 [1:]
print (data2)
with open('data.txt', 'r') as f:
col3 = [line.split()[2] for line in f]
data3 = col3 [1:]
print (data3)
结果如下:
['A', 'B', 'C', 'D', 'E']
['15.5', '18.9', '17.4', '11.4', '23.4']
['55.7', '51.6', '67.3', '34.5', '92.1']
但是,我想将 data2 和 data3 作为浮点数列表。如何更正上述程序?任何帮助,请。