Adding a column in a tab delimited txt file

Blake Garretson find.me at my.homepage.net
Thu Aug 21 22:21:16 EDT 2003


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





More information about the Python-list mailing list