[Tutor] Parsing large files
Karl Pflästerer
sigurd at 12move.de
Fri Feb 6 20:01:52 EST 2004
On 7 Feb 2004, Andrew Eidson <- abeidson at sbcglobal.net wrote:
> well thanks everyone.. I found a simple way of doing this through
> Linux..
> cut -f1-86 infile > outfile
> I am still playing with the CSV import but still have not found decent
> documentation to help me with it.
Maybe I overlooking something but you wrote the fields are tab
separated. Why don't you simply write:
def split_cols (infile, cols, outfile1, outfile2):
inf = file(infile)
of1 = file(outfile1, "w")
of2 = file(outfile2, "w")
for line in inf:
line = line.split('\t')
print >> of1, '\t'.join(line[:cols])
of2.write('\t'.join(line[cols:]))
inf.close()
of1.close()
of2.close()
That will write the columns to the two files.
If you like generators you could also wrote:
def split_cols (infile, cols, outfile1, outfile2):
def splitter (fil, cols):
for line in fil:
line = line.split('\t')
yield ('\t'.join(line[:cols]) + '\n',
'\t'.join(line[cols:]))
inf = file(infile)
of1 = file(outfile1, "w")
of2 = file(outfile2, "w")
for left, right in splitter(inf, cols):
of1.write(left)
of2.write(right)
inf.close()
of1.close()
of2.close()
Karl
--
Please do *not* send copies of replies to me.
I read the list
More information about the Tutor
mailing list