11
votes

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:

(*,224.0.0.0/4) RPF nbr: 96.34.35.36 Flags: C RPF P
  Up: 1w6d

(*,224.0.0.0/24) Flags: D P
  Up: 1w6d

(*,224.0.1.39) Flags: S P
  Up: 1w6d

(96.34.246.55,224.0.1.39) RPF nbr: 96.34.35.36 Flags: RPF
  Up: 1w5d
  Incoming Interface List
    Bundle-Ether434 Flags: F A, Up: 1w5d
  Outgoing Interface List
    BVI100 Flags: F, Up: 1w5d
    TenGigE0/0/0/3 Flags: F, Up: 1w5d
    TenGigE0/0/1/1 Flags: F, Up: 1w5d
    TenGigE0/0/1/2 Flags: F, Up: 1w5d
    TenGigE0/0/1/3 Flags: F, Up: 1w5d
    TenGigE0/1/1/1 Flags: F, Up: 1w5d
    TenGigE0/1/1/2 Flags: F, Up: 1w5d
    TenGigE0/2/1/0 Flags: F, Up: 1w5d
    TenGigE0/2/1/1 Flags: F, Up: 1w5d
    TenGigE0/2/1/2 Flags: F, Up: 1w5d
    Bundle-Ether234 (0/3/CPU0) Flags: F, Up: 3d16h
    Bundle-Ether434 Flags: F A, Up: 1w5d

I want to split on anything that is a new line online and only a newline.

Example code is below:

myarray = []
myarray = output.split("\n")
for line in myarray:
    print line
    print "Next Line"

I am do have the "re" library imported.

3
So... You just want to split on two newlines in a row? A line with nothing but a newline on it is just two newlines, isn't it? - James

3 Answers

19
votes

It's quite easy when you consider what is on an empty line. It's just the newline character, so splitting on an empty line would be splitting on two newline characters in sequence (one from the previous non-empty line, one is the 'whole' empty line.

myarray = output.split("\n\n")
for line in myarray:
    print line
    print "Next Line"

and for Python 3:

myarray = output.split("\n\n")
for line in myarray:
    print(line)
    print("Next Line")

If you want to be platform-agnostic, use os.linesep + os.linesep instead of "\n\n", as is mentioned in Lost's answer.

7
votes

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.

  1. Firstly, the greedy matching means that many blank lines count as a single match, i.e. 6 blank lines makes one split, not three splits.
  2. Secondly, the pattern doesn't just match \n but either \r\n (for Windows) or \n (for Linux/Mac).
  3. Thirdly, the group (denoted by parentheses) needs to have ?: inside the
    opening parenthesis to make it a "non-capturing" group, which changes the behaviour of re.split.

For example:

s = """

hello
world

this is







a test

"""

split_on_empty_lines(s)

returns

['hello\nworld', 'this is', 'a test']
2
votes

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