Text processing and file creation

George Sakkis george.sakkis at gmail.com
Thu Sep 6 22:50:39 EDT 2007


On Sep 5, 5:17 pm, "malibus... at gmail.com" <malibus... at gmail.com>
wrote:
> On Sep 5, 1:28 pm, Paddy <paddy3... at googlemail.com> wrote:
>
> > On Sep 5, 5:13 pm, "malibus... at gmail.com" <malibus... at gmail.com>
> > wrote:
>
> > > I have a text source file of about 20.000 lines.>From this file, I like to write the first 5 lines to a new file. Close
>
> > > that file, grab the next 5 lines write these to a new file... grabbing
> > > 5 lines and creating new files until processing of all 20.000 lines is
> > > done.
> > > Is there an efficient way to do this in Python?
> > > In advance, thanks for your help.
>
> > If its on unix: use split.
> > If its your homework: show us what you have so far...
>
> > - Paddy.
>
> Paddy,
>
> Thanks for making me aware of the (UNIX) split command (split -l 5
> inFile.txt), it's short, it's fast, it's beautiful.
>
> I am still wondering how to do this efficiently in Python (being kind
> of new to it... and it's not for homework).
>
> -- Martin.
>
> I am still wondering how to do this in Python (being new to Python)

If this was a code golf challenge, a decent entry (146 chars) could
be:

import itertools as it
for i,g in it.groupby(enumerate(open('input.txt')),lambda(i,_):i/
5):open("output.%d.txt"%i,'w').writelines(s for _,s in g)

or a bit less cryptically:

import itertools as it
for chunk,enum_lines in it.groupby(enumerate(open('input.txt')),
                                   lambda (i,line): i//5):
  open("output.%d.txt" % chunk, 'w').writelines(line for _,line
                                                in enum_lines)


George




More information about the Python-list mailing list