Adding a column in a tab delimited txt file
-
madsurfer2000 at hotmail.com
Fri Aug 22 10:47:33 EDT 2003
gcf78 at hotmail.com (Garry) wrote in message news:<b3fb3ec2.0308211636.4d0b99fc at posting.google.com>...
> 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))
More information about the Python-list
mailing list