Python 在没有空格的行前拆分段落

Python 在没有空格的行前拆分段落,python,split,Python,Split,我需要像下面那样分开 eth 0,地址001,以太网类型 设备本机,已建立强制转换。 真正公平,没有备份 em 1,地址002,以太网类型 设备本机,强制转换退出 eth 2,地址003,以太网类型 设备本机,强制转换为0 b0 页面设备,本机:操作$ 环回3,地址003,以太网类型 设备本机 逻辑:eth,em,环回。。所有的一切都是在没有任何空间的情况下开始的。中间数据之前可能有多个空格。我需要这种逻辑 挑战:这不是强制要求在每段末尾有一个特定的模式 这是一个更具体的问题 您可以使用该

我需要像下面那样分开

eth 0,地址001,以太网类型
设备本机,已建立强制转换。
真正公平,没有备份
em 1,地址002,以太网类型
设备本机,强制转换退出
eth 2,地址003,以太网类型
设备本机,强制转换为0 b0
页面设备,本机:操作$
环回3,地址003,以太网类型
设备本机

逻辑:eth,em,环回。。所有的一切都是在没有任何空间的情况下开始的。中间数据之前可能有多个空格。我需要这种逻辑

挑战:这不是强制要求在每段末尾有一个特定的模式

这是一个更具体的问题

您可以使用该函数进行拆分 换行符处的多行字符串

对于在其他字符处拆分行,请使用 功能

输出:

data = '''
eth 0, address 001, type ethernet
 device native, cast established.
  real fair, backup none
em 1, address 002, type ethernet
  device native, cast exit
eth 2, address 003, type ethernet
  device native, cast rescue0b0
  page device, native: action$
loopback 3, address 003, type ethernet
   device native
'''

splittedLines = data.splitlines()

for word in splittedLines:
    print(word)
import re
pattern=r'^\S[\s\S]+?(?=^\S|\n{2,}|\Z)'
string_1='''eth 0, address 001, type ethernet
 device native, cast established.
  real fair, backup none
em 1, address 002, type ethernet
  device native, cast exit
eth 2, address 003, type ethernet
  device native, cast rescue0b0
  page device, native: action$
loopback 3, address 003, type ethernet
   device native'''

match=re.finditer(pattern,string_1,re.M)
for find in match:
    print(find.group())

您可以在这里尝试一些正则表达式:

eth 0, address 001, type ethernet
 device native, cast established.
  real fair, backup none

em 1, address 002, type ethernet
  device native, cast exit

eth 2, address 003, type ethernet
  device native, cast rescue0b0
  page device, native: action$

loopback 3, address 003, type ethernet
   device native
输出:

data = '''
eth 0, address 001, type ethernet
 device native, cast established.
  real fair, backup none
em 1, address 002, type ethernet
  device native, cast exit
eth 2, address 003, type ethernet
  device native, cast rescue0b0
  page device, native: action$
loopback 3, address 003, type ethernet
   device native
'''

splittedLines = data.splitlines()

for word in splittedLines:
    print(word)
import re
pattern=r'^\S[\s\S]+?(?=^\S|\n{2,}|\Z)'
string_1='''eth 0, address 001, type ethernet
 device native, cast established.
  real fair, backup none
em 1, address 002, type ethernet
  device native, cast exit
eth 2, address 003, type ethernet
  device native, cast rescue0b0
  page device, native: action$
loopback 3, address 003, type ethernet
   device native'''

match=re.finditer(pattern,string_1,re.M)
for find in match:
    print(find.group())

您只需要在以非空格字符开头的每一行(第一行除外?)之前添加一个额外的新行,对吗?到目前为止,您尝试了什么?打印(“1”+字)输出========1第1个0,地址001,键入以太网1设备本机,强制转换已建立。1真实公平,备份无我需要的=1第1个0,地址001,键入以太网设备本机,已建立强制转换。real fair,无备份1em 1,地址002,类型ethernet@NishantThakur你能详细说明你想做什么吗?太好了,非常感谢,我在这里得到了我的输出。我是新来的。感谢您解释每一个表达,以便我能进一步学习。@NishantThakur,请参阅此处右侧的解释:)完美。。。非常感谢你的帮助。\n{2,}在这里有什么用?@NishantThakur\n匹配新行提要和{2,}量词匹配在2到无限次之间。