Skip to content

Instantly share code, notes, and snippets.

@n8henrie
Created August 20, 2015 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n8henrie/35e6a8200d315f43ed39 to your computer and use it in GitHub Desktop.
Save n8henrie/35e6a8200d315f43ed39 to your computer and use it in GitHub Desktop.
Split lines of a text file by a rule (delimiter) and add the delimiter either before or after matching chunks of text.
def gen_groups(iterable, test, to_beginning=True):
group = []
for line in iterable:
# If the line is a delimiter
if test(line):
# and you want delimiters to start groups
if to_beginning:
# And there is already a group that has
# been accumulating non-delimiters
if group:
# Yield the group
yield group
# Set group to a list of just the current line
# since to_beginning is true and the line is
# a delimiter. Now ready to accumulate more lines.
group = [line]
# Line is a delimiter, but we want to put delimiters
# at the end of the group
else:
# Empty list + non-empty list = non-empty list, or
# return existing group with delimiter tagged onto
# the end
yield group + [line]
# Reset group for next run
group = []
# Line is not a delimiter...
else:
# ...so add it to the previous group
group.append(line)
# Yield anything left that didn't get yielded before.
if group:
yield group
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment