Trying to use a python split on a \"empty\" newline but not any other new lines. I tried a few other example I found but none of them seem to work.
Data example:
This works in the case where multiple blank lines should be treated as one.
import re
def split_on_empty_lines(s):
# greedily match 2 or more new-lines
blank_line_regex = r"(?:\r?\n){2,}"
return re.split(blank_line_regex, s.strip())
The regex is a bit odd.
\n
but either \r\n
(for
Windows) or \n
(for Linux/Mac). ?:
inside there.split
.For example:
s = """
hello
world
this is
a test
"""
split_on_empty_lines(s)
returns
['hello\nworld', 'this is', 'a test']
A blank line is just two new lines. So your easiest solution is probably to check for two new lines (UNLESS you expect to have a situation where you'll have more than two blank lines in a row).
import os
myarray = [] #As DeepSpace notes, this is not necessary as split will return a list. No impact to later code, just more typing
myarray = output.split(os.linesep + os.linesep) ##use os.linesep to make this compatible on more systems
That would be where I'd start anyway
It's quite easy when you consider what is on empty line. It's just the the newline character, so splitting on empty line would be splitting on two newline characters in sequence (one from the previou non-empty line, one is the 'whole' empty line.
myarray = output.split("\n\n")
for line in myarray:
print line
print "Next Line"