Yumnam_G
Yumnam_G

Reputation: 25

Reading datas from a .dat file into different variables in python

I have a file.dat which looks like the following:

0.00000000000000000000 0.00000000000000000000 0.00000000000000000000
1.02072021799999990144 0.58931309249999996869 0.01464322176999999919
1.04801084399999999697 0.60506934300000003724 0.02121744689000000170
1.11938267900000010258 0.64627589110000005501 0.03132390833999999791
1.23483727700000001093 0.71293363409999999103 0.03462252470999999804
1.36316318200000008432 0.78702263009999995358 0.02774525518999999829
1.44821317199999999303 0.83612626459999994655 0.01540671738000000054

I want to create 3 variables x, y, z such that x would be an array with the first column, y with the second column and z with the third column. I have tried with the following:

with open('filename') as f:
     file = f.read()

     x = [row.split(' ')[0] for row in data]
     y = [row.split(' ')[1] for row in data]
     z = [row.split(' ')[2] for row in data]

However, this is not working and it gives an error. Is there any simple and easy way to perform this?

Upvotes: 1

Views: 1737

Answers (3)

Remi Guan
Remi Guan

Reputation: 22282

  1. f.read() return the whole file. I think you need split them, use str.splitlines().

  2. file = f.read() but for row in data? I think you meant data = f.read().

  3. If you're looking for a more simply way, I'd suggest:

    with open('filename') as f:
        data = f.read().splitlines()
        x, y, z = zip(*[i.split() for i in data])
    

    Or just don't define that data variable:

    with open('filename') as f:
        x, y, z = zip(*[line.split() for line in f])
    

    Which gives you:

    >>> print(x)
    ('0.00000000000000000000', '1.02072021799999990144', '1.04801084399999999697', '1.11938267900000010258', '1.23483727700000001093', '1.36316318200000008432', '1.44821317199999999303')
    >>> print(y)
    ('0.00000000000000000000', '0.58931309249999996869', '0.60506934300000003724', '0.64627589110000005501', '0.71293363409999999103', '0.78702263009999995358', '0.83612626459999994655')
    >>> print(z)
    ('0.00000000000000000000', '0.01464322176999999919', '0.02121744689000000170', '0.03132390833999999791', '0.03462252470999999804', '0.02774525518999999829', '0.01540671738000000054')
    >>> 
    

Upvotes: 2

Kasravnd
Kasravnd

Reputation: 107297

First of all as a more pythonic way you can use numpy.genfromtxt to read your text as an array, and then get the columns by transposing the array :

import numpy as np

with open('text.dat') as f:
    array = np.genfromtxt(f)
    x,y,z = array.T

And about your code, it's not clear that what's data, anyway if you want to loop over the lines of your file you need to simple loop over file object:

x = []
y = []
z = []
with open('filename') as f:
    for line in f:
     i,j,k = line.split()
        x .append(i)
        y .append(j)
        z .append(k)

Or as a more elegant way use zip() function to get the columns :

with open('text.dat') as f:
     x, y, z = zip(*[line.split() for line in f])

Upvotes: 2

sameera sy
sameera sy

Reputation: 1718

Try this. split() across the row and append it onto list, something like this. It's simple though

x=[]
y=[]
z=[]
with open('file.txt') as handler:
    for line in handler:
        parts = line.split()
        x.append(parts[0]) 
        y.append(parts[1])
        z.append(parts[2]) 
print x,y,z

x y and z contain the first second and third row respectively

Upvotes: 1

Related Questions