Text processing and file creation

Alberto Griggio alberto.griggio at gmail.com
Thu Sep 6 05:21:41 EDT 2007


> 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).

Something like this should do the job:

def nlines(num, fileobj):
    done = [False]
    def doit():
        for i in xrange(num):
            l = fileobj.readline()
            if not l:
                done[0] = True
                return
            yield l
    while not done[0]:
        yield doit()

for i, group in enumerate(nlines(5, open('bigfile.txt'))):
    out = open('chunk_%d.txt' % i)
    for line in group:
        out.write(line)


> I am still wondering how to do this in Python (being new to Python)

This is just one way of doing it, but not as concise as using split...

Alberto





More information about the Python-list mailing list