Reputation: 71
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
'''
i need to split like below.
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
logic: eth, em, loopback .. all start without any space. and there could be multiple space before middle data. i need that logic
challenge: this is not mandatory to have a specific pattern at the end of each paragraph.
this is more specific question of split paragraphs with a line which is start without space and lookup the data perline in split data
Upvotes: 0
Views: 77
Reputation: 12669
You can try some regex here :
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())
Output:
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
Upvotes: 1
Reputation: 5853
You can use the string.splitlines() function to split multi-line string at newlines.
For splitting lines at other characters use string.split() function.
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)
Output:
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
Upvotes: 0