Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. @invalid-email-address Anonymous created this gist Jan 1, 2017.
    28 changes: 28 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #!/usr/bin/env python

    def split_into_groups(iterable, group_size):
    """Split an iterable collection into groups with fixed size.
    Yield
    -----
    list[any]
    Groups of elements.
    """
    buf = []
    it = iter(iterable)
    try:
    while True:
    for i in range(group_size):
    buf.append(next(it))
    yield buf
    del buf[:]
    except StopIteration:
    pass
    if buf:
    yield buf


    with open('input.txt', 'rb') as fin:
    for i, lines in enumerate(split_into_groups(fin, group_size=10000)):
    with open('output%d.txt' % i, 'wb') as fout:
    fout.write(b''.join(lines))