尝试在“空”换行符而不是任何其他换行符上使用 python 拆分。我尝试了其他几个我发现的例子,但似乎都不起作用。
数据示例:
(*,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"
我确实导入了“re”库。
最佳答案
当您考虑空行上的内容时,这很容易。它只是换行符,所以在一个空行上拆分将按顺序拆分两个换行符(一个来自前一个非空行,一个是“整个”空行。
myarray = output.split("\n\n")
for line in myarray:
print line
print "Next Line"
对于 Python 3:
myarray = output.split("\n\n")
for line in myarray:
print(line)
print("Next Line")
如果您想与平台无关,请使用 os.linesep + os.linesep
而不是 "\n\n"
,如 Lost 的回答中所述。
关于空新行上的 Python "split",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38852712/