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.
There is no need of reading the file 3 times here, you can do this by defining a simple function that returns float value of the the item if it is a valid number otherwise returns it as it is.
Now read all the lines one by one using a list comprehension and apply this function to the items of each line. So now you've a list of lists, and it's time to unzip that list of lists using zip(*)
and assign the return value to data1
, data2
, data3
def ret_float(x):
try:
return float(x)
except ValueError:
return x
with open('data.txt') as f:
next(f) #skip the header
lis = [ map(ret_float,line.split()) for line in f]
#[['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]]
#unzip the list
data1, data2, data3 = zip(*lis)
#if you want data1,data2,data3 to be lists then use:
#data1, data2, data3 = [list(x) for x in zip(*lis)]
...
>>> data1
('A', 'B', 'C', 'D', 'E')
>>> data2
(15.5, 18.9, 17.4, 11.4, 23.4)
>>> data3
(55.7, 51.6, 67.3, 34.5, 92.1)
Update : Fixing your solution
with open('data.txt', 'r') as f:
col2 = [line.split()[1] for line in f]
data2 = list(map(float, col2 [1:])) # apply float to each item using `map`
# as `map` returns a `map` object in py3.x
# you have to pass it to list()
with open('data.txt', 'r') as f:
col3 = [line.split()[2] for line in f]
data3 = list(map(float, col3 [1:]))
print (data3)
help on map
:
>>> print(map.__doc__)
map(func, *iterables) --> map object
Make an iterator that computes the function using arguments from
each of the iterables. Stops when the shortest iterable is exhausted.
Use the float()
function. It takes 1 arg, which would be the string/var you want to turn into a float.
Use the float()
command:
with open('data.txt', 'r') as f:
col2 = [float(line.split()[1]) for line in f]
data2 = col2[1:]
print(data2)
Convert the strings to float
as you read them:
data1 = col1 [1:]
becomes
data1 = float(col1 [1:])
But--and I know you didn't ask this--your approach is a little sideways. You might want to consider a program structure more like this:
data0 = []
data1 = []
data2 = []
with open('data.txt', 'r') as f:
f.readline()
for line in f:
vals = line.split()
data0.append(vals[0])
data1.append(vals[1])
data2.append(vals[2])
print(data0)
print(data1)
print(data2)
Hope it helps!
(Disclaimer: works with Python 2.7; not sure about 3.x.)
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 = float(col2 [1:])
print (data2)
with open('data.txt', 'r') as f:
col3 = [line.split()[2] for line in f]
data3 = float(col3 [1:])
print (data3)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.