Adding a column in a tab delimited txt file

Discussion in 'Python' started by Garry, Aug 22, 2003.

  1. Garry

    Garry Guest

    Hi, I am new to python, hope someone can help me here:
    I have a MS Access exported .txt file which is tab delimited in total
    20 columns, now I need to add another column of zero at the 4th column
    position and a column of zero at the 9th column position. What is the
    best way to do this? Can I write a while loop to count the number of
    tab I hit until the counter is 4 and then add a zero in between and
    thru the whole file?

    Thanks,
    Garry
     
    Garry, Aug 22, 2003
    #1
    1. Advertising

  2. Garry wrote:
    > Can I write a while loop to count the number of
    > tab I hit until the counter is 4 and then add a zero in between and
    > thru the whole file?


    Try using the split string method and then the insert list method.

    Untested code:

    ###
    infile = file("in.txt")
    outfile = file("out.txt")

    for line in f:
    columns = line.split("\t")
    columns.insert(4, "0")
    outfile.write("\t".join(columns)+"\n")
    ###

    The code might not be perfect, but you get the idea.

    --------------------------------------------------
    Blake T. Garretson http://blakeg.freeshell.org
     
    Blake Garretson, Aug 22, 2003
    #2
    1. Advertising

  3. Blake Garretson wrote:
    > for line in f:


    I meant "for line in infile:"

    Sorry!

    --------------------------------------------------
    Blake T. Garretson http://blakeg.freeshell.org
     
    Blake Garretson, Aug 22, 2003
    #3
  4. Garry

    John Hunter Guest

    >>>>> "Garry" == Garry <> writes:

    Garry> Hi, I am new to python, hope someone can help me here: I
    Garry> have a MS Access exported .txt file which is tab delimited
    Garry> in total 20 columns, now I need to add another column of
    Garry> zero at the 4th column position and a column of zero at the
    Garry> 9th column position. What is the best way to do this? Can I
    Garry> write a while loop to count the number of tab I hit until
    Garry> the counter is 4 and then add a zero in between and thru
    Garry> the whole file?

    Unless the file is terribly large, it will be easier to slurp the
    whole thing into memory, manipulate some list structures, and then
    dump back to the file.

    There are a couple of nifty things to speed you along. You can use
    string split methods to split the file on tabs and read the file into
    a list of rows, each row split on the tabs.

    rows = [line.split('\t') for line in file('tabdelim.dat')]

    The next fun trick is to use the zip(*rows) to tranpose this into a
    list of columns. You can then use the list insert method to insert
    your column. Here I'm adding a last name column to the third column.

    cols = zip(*rows) # transposes 2Dlist
    cols.insert(2, ['Hunter', 'Sierig', 'Hunter', 'Hunter'])

    Now all that is left is to transpose back to rows and write the new
    file using the string method join to rejoin the columns with tabs

    rows = zip(*cols) # transpose back
    file('newfile.dat', 'w').writelines(['\t'.join(row) for row in rows])

    This script takes an input file like

    1 John 35 M
    2 Miriam 31 F
    3 Rahel 5 F
    4 Ava 2 F

    and generates an outfile

    1 John Hunter 35 M
    2 Miriam Sierig 31 F
    3 Rahel Hunter 5 F
    4 Ava Hunter 2 F

    Damn cool!

    Here is the whole script:

    rows = [line.split('\t') for line in file('tabdelim.dat')]
    cols = zip(*rows)
    cols.insert(2, ['Hunter', 'Sierig', 'Hunter', 'Hunter'])
    rows = zip(*cols)
    file('newfile.dat', 'w').writelines(['\t'.join(row) for row in rows])

    Cheers,
    John Hunter
     
    John Hunter, Aug 22, 2003
    #4
  5. Garry

    - Guest

    (Garry) wrote in message news:<>...
    > Hi, I am new to python, hope someone can help me here:
    > I have a MS Access exported .txt file which is tab delimited in total
    > 20 columns, now I need to add another column of zero at the 4th column
    > position and a column of zero at the 9th column position. What is the
    > best way to do this?


    I don't know the best way, but one way is this.

    import re

    infile = file("in.txt","r")
    outfile = file("out.txt","w")

    pattern = re.compile(r'^((?:[^\t]+\t){3})((?:[^\t]+\t){5})')
    replace = '\g<1>0\t\g<2>0\t'

    for line in infile:
    outfile.write(pattern.sub(replace,line))
     
    -, Aug 22, 2003
    #5
    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. DC Gringo
    Replies:
    2
    Views:
    1,932
    DC Gringo
    Apr 25, 2005
  2. David Lozzi

    Export to Tab Delimited Text File

    David Lozzi, Mar 31, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    5,003
    David Lozzi
    Apr 1, 2006
  3. RyanL
    Replies:
    6
    Views:
    729
    Paul McGuire
    Aug 28, 2007
  4. Replies:
    1
    Views:
    382
    ZedGama3
    Apr 14, 2004
  5. Srikant
    Replies:
    1
    Views:
    543
    Greg Bacon
    Sep 29, 2007
Loading...

Share This Page