I have a text file (data.txt) delimited by tab as follows:
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
The program below gives the result as the list of strings.
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)
The results are as follows:
['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']
But, I want to get data2 and data3 as the list of floats. How can I correct above program? Any help, please.