简体   繁体   English

Python在空的新行上“拆分”

[英]Python “split” on empty new line

Trying to use a python split on a "empty" newline but not any other new lines.尝试在“空”换行符上使用 python 拆分,而不是任何其他新行。 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.我确实导入了“re”库。

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:对于 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.如果您想与平台无关,请使用os.linesep + os.linesep而不是"\\n\\n" ,如 Lost 的答案中所述。

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, ie 6 blank lines makes one split, not three splits.首先,贪心匹配意味着许多空行算作一次匹配,即6个空行构成一个分裂,而不是三个分裂。
  2. Secondly, the pattern doesn't just match \\n but either \\r\\n (for Windows) or \\n (for Linux/Mac).其次,模式不仅匹配\\n还匹配\\n \\r\\n (对于 Windows)或\\n (对于 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 . re.split括号使其成为“非捕获”组,这会改变re.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无论如何,那将是我开始的地方

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM