0

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.

2
  • @Nolen yes but data2 = map(float, data2) resulted <map object at 0x02E6EDF0> Commented Jun 12, 2013 at 18:47
  • This looks like a tsv file - have you considered using the [CSV module ](docs.python.org/3.3/library/csv.html) and changing the delimiter to tabs? You'll only have to open your file once to access all your columns. Commented Jun 12, 2013 at 18:57

5 Answers 5

3

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.
Sign up to request clarification or add additional context in comments.

11 Comments

is there difference between (15.5, 18.9, 17.4, 11.4, 23.4) and [15.5, 18.9, 17.4, 11.4, 23.4]?
@monica yes first one is a tuple and second one is a list. Use : data1, data2, data3 = [list(x) for x in zip(*lis)] if you want lists.
@AshwiniChaudhary so i want list
@monica I've added that to my solution.
@AshwiniChaudhary Does not my question has alternative much simpler solution?
|
0

Use the float() function. It takes 1 arg, which would be the string/var you want to turn into a float.

Comments

0

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)

Comments

0
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)

1 Comment

['A', 'B', 'C', 'D', 'E'] Traceback (most recent call last): File "C:/Users/ed/Desktop/tt.py", line 7, in <module> data2 = float(col2 [1:]) TypeError: float() argument must be a string or a number
0

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.)

1 Comment

@ron.rothmanno, Traceback (most recent call last): File "C:/Users/ed/Desktop/tt.py", line 5, in <module> for line in f[1:]: TypeError: '_io.TextIOWrapper' object is not subscriptable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.