Reading a tab delimited text file.

Discussion in 'Python' started by Stephen, Feb 23, 2009.

  1. Stephen

    Stephen Guest

    Hi,

    I would like to read a text file of numbers produced by a data
    acquisition system into three vectors of doubles. The contents of the
    file are:

    +0.0000000e+0 +2.7645134e+1 +2.7745625e+1

    +0.4100041e-1 +2.7637787e+1 +2.7731047e+1

    +0.0820008e+0 +2.7645134e+1 +2.7750483e+1
    ....

    or

    +0.0000000e+0\t+2.7645134e+1\t+2.7745625e+1\r\n
    ....

    I would like to read the first column into time_vec, second into
    ch1_vec and so on.

    (I have never programmed in python and am having trouble finding a way
    to do this).

    Thanks
    Stephen, Feb 23, 2009
    #1
    1. Advertising

  2. Stephen

    Tim Chase Guest

    > I would like to read a text file of numbers produced by a data
    > acquisition system into three vectors of doubles. The contents of the
    > file are:
    >
    > +0.0000000e+0 +2.7645134e+1 +2.7745625e+1
    > +0.4100041e-1 +2.7637787e+1 +2.7731047e+1
    > +0.0820008e+0 +2.7645134e+1 +2.7750483e+1
    > ...
    >
    > or
    >
    > +0.0000000e+0\t+2.7645134e+1\t+2.7745625e+1\r\n
    > ...
    >
    > I would like to read the first column into time_vec, second into
    > ch1_vec and so on.
    >
    > (I have never programmed in python and am having trouble finding a way
    > to do this).


    Well, a very terse way of doing it would be something like:

    time_vec, ch1_vec, and_so_on = zip(*(
    map(float, line.split())
    for line in file('in.txt')))

    If my junior developer authored that, I'm not sure whether I'd
    laud her or fire her. :)

    If this isn't homework, there are some less terse versions which
    are a bit easier on the eyes and less like some love-child
    between Perl and Python.

    -tkc
    Tim Chase, Feb 23, 2009
    #2
    1. Advertising

  3. Stephen

    Stephen Guest

    On Feb 23, 4:06 pm, Tim Chase <> wrote:
    > > I would like to read a text file of numbers produced by a data
    > > acquisition system into three vectors of doubles. The contents of the
    > > file are:

    >
    > > +0.0000000e+0      +2.7645134e+1   +2.7745625e+1
    > > +0.4100041e-1      +2.7637787e+1   +2.7731047e+1
    > > +0.0820008e+0      +2.7645134e+1   +2.7750483e+1
    > > ...

    >
    > > or

    >
    > > +0.0000000e+0\t+2.7645134e+1\t+2.7745625e+1\r\n
    > > ...

    >
    > > I would like to read the first column into time_vec, second into
    > > ch1_vec and so on.

    >
    > > (I have never programmed in python and am having trouble finding a way
    > > to do this).

    >
    > Well, a very terse way of doing it would be something like:
    >
    >    time_vec, ch1_vec, and_so_on = zip(*(
    >      map(float, line.split())
    >      for line in file('in.txt')))
    >
    > If my junior developer authored that, I'm not sure whether I'd
    > laud her or fire her. :)
    >
    > If this isn't homework, there are some less terse versions which
    > are a bit easier on the eyes and less like some love-child
    > between Perl and Python.
    >
    > -tkc


    haha, no this isn't homework. I'm a mechanical engineering student
    working on a research project and this program is for my personal use
    to analyze the data.
    Stephen, Feb 23, 2009
    #3
  4. Stephen

    Mel Guest

    Stephen wrote:

    > Hi,
    >
    > I would like to read a text file of numbers produced by a data
    > acquisition system into three vectors of doubles. The contents of the
    > file are:
    >
    > +0.0000000e+0 +2.7645134e+1 +2.7745625e+1
    >
    > +0.4100041e-1 +2.7637787e+1 +2.7731047e+1
    >
    > +0.0820008e+0 +2.7645134e+1 +2.7750483e+1
    > ...
    >
    > or
    >
    > +0.0000000e+0\t+2.7645134e+1\t+2.7745625e+1\r\n
    > ...
    >
    > I would like to read the first column into time_vec, second into
    > ch1_vec and so on.
    >
    > (I have never programmed in python and am having trouble finding a way
    > to do this).


    A simple starting point, without error handling, etc., could be:

    a_list = []
    b_list = []
    c_list = []
    for line in open ('my_numbers.txt', 'rt'):
    a, b, c = [float (x) for x in line.split()]
    a_list.append (a)
    b_list.append (b)
    c_list.append (c)

    Assuming every line has three tab-separated numbers, the text versions of
    the numbers are all convertable to float, the text file that's opened will
    eventually be closed (whether by garbage collection or program
    termination.)

    This code does illustrate the string's split method, list comprehension,
    sequence unpacking, and the fact that each of a_list, b_list, c_list *MUST*
    be initialized with a distinct empty list object.

    Mel.
    Mel, Feb 23, 2009
    #4
  5. Stephen

    Tim Chase Guest

    >> time_vec, ch1_vec, and_so_on = zip(*(
    >> map(float, line.split())
    >> for line in file('in.txt')))
    >>
    >> If this isn't homework, there are some less terse versions which
    >> are a bit easier on the eyes and less like some love-child
    >> between Perl and Python.

    >
    > haha, no this isn't homework. I'm a mechanical engineering student
    > working on a research project and this program is for my personal use
    > to analyze the data.


    The "zip-star map-float" variant is a pretty unreadable way to go.

    The more readable versions look something like

    data = [map(float, line.split()) for line in file('in.txt')]
    time_vec = [bit[0] for bit in data]
    ch1_vec = [bit[1] for bit in data]
    and_so_on = [bit[2] for bit in data]

    or even

    time_vec = []
    ch1_vec = []
    and_so_on = []
    for line in file('in.txt'):
    a,b,c = map(float, line.split())
    time_vec.append(a)
    ch1_vec.append(b)
    and_so_on.append(c)

    which could also be written as

    for line in file('in.txt'):
    line = line.split()
    time_vec.append(float(line[0]))
    ch1_vec.append(float(line[1]))
    and_so_on.append(float(line[2]))

    -tkc
    Tim Chase, Feb 23, 2009
    #5
  6. Stephen

    harijay Guest

    You could also use the csv module

    import csv
    myfileobj = open("myfiletab.txt","read")
    csv_read = csv.reader(myfileobj,dialect=csv.excel_tab)
    myval1 = []
    myval2 = []
    myval3 = []
    for line in csv_read:
    # filter header and stuff using some criterion
    if len(line) = 3:
    myval1.append(line[0])
    myval2.append(line[1])
    myval3.append(line[2])


    etc etc . The csv module defines a dialect excel_tab. Each line is
    then parsed into an array . You can insert that array into another
    array and have an array line mydata = [ [line1],[line2] ...]

    Hopw this helps
    harijay

    On Feb 23, 5:45 pm, Tim Chase <> wrote:
    > >>    time_vec, ch1_vec, and_so_on = zip(*(
    > >>      map(float, line.split())
    > >>      for line in file('in.txt')))

    >
    > >> If this isn't homework, there are some less terse versions which
    > >> are a bit easier on the eyes and less like some love-child
    > >> between Perl and Python.

    >
    > > haha, no this isn't homework. I'm a mechanical engineering student
    > > working on a research project and this program is for my personal use
    > > to analyze the data.

    >
    > The "zip-star map-float" variant is a pretty unreadable way to go.
    >
    > The more readable versions look something like
    >
    >    data = [map(float, line.split()) for line in file('in.txt')]
    >    time_vec = [bit[0] for bit in data]
    >    ch1_vec = [bit[1] for bit in data]
    >    and_so_on = [bit[2] for bit in data]
    >
    > or even
    >
    >    time_vec = []
    >    ch1_vec = []
    >    and_so_on = []
    >    for line in file('in.txt'):
    >      a,b,c = map(float, line.split())
    >      time_vec.append(a)
    >      ch1_vec.append(b)
    >      and_so_on.append(c)
    >
    > which could also be written as
    >
    >    for line in file('in.txt'):
    >      line = line.split()
    >      time_vec.append(float(line[0]))
    >      ch1_vec.append(float(line[1]))
    >      and_so_on.append(float(line[2]))
    >
    > -tkc


    Another way would be to use the csv module .

    import csv
    f = file.open("mytabfile.txt"
    harijay, Feb 23, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Export to Tab Delimited Text File

    David Lozzi, Mar 31, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    4,925
    David Lozzi
    Apr 1, 2006
  2. Replies:
    5
    Views:
    4,827
  3. Gibson
    Replies:
    2
    Views:
    1,407
    Gibson
    Nov 19, 2008
  4. Jerry Coffin
    Replies:
    2
    Views:
    918
  5. nick
    Replies:
    2
    Views:
    419
Loading...

Share This Page