I've asked a similar question in the past, but I'm not good at it, so I'll ask you again.
Here is sample textfile.txt
dummy01234567890
0987654321dummy
-------start-------(It is possible to modify)
text line1
text line2
-------end---------(It is possible to modify)
12345678910
qwertyuiop
-------start-------(It is possible to modify)
text line3
text line4
-------end---------(It is possible to modify)
;p12309809128309123
dummyline1235567
I want to parse
"text line 1\\n text line 2" → array [0]
"text line 3\\n text line 4" → array [1]
How should I source coding in python?
Should I use split function twice?
Finite-state machine is adaptive and simple enough for most needs.
state = 'init'
arrays = []
with open('textfile.txt') as f:
lines = []
for line in f.readlines():
if state == 'init': # seek for start
word = line.strip().strip('-')
if word != 'start':
continue
state = 'start'
lines = []
elif state == 'start': # start parsing now
word = line.strip().strip('-')
if word != 'end':
lines.append(line.strip())
continue
# end current parsing now
arrays.append('\n'.join(lines))
state = 'init'
You can do something like this to achieve the desired result :
text = """dummy01234567890
0987654321dummy
-------start-------(It is possible to modify)
text line1
text line2
-------end---------(It is possible to modify)
12345678910
qwertyuiop
-------start-------(It is possible to modify)
text line3
text line4
-------end---------(It is possible to modify)
;p12309809128309123
dummyline1235567"""
text_list = text.splitlines()
print(['\n'.join([text_list[3+i*6].strip(), text_list[4+i*6].strip()]) for i in xrange(len(text_list)/6)])
This will result in :
['text line1\ntext line2', 'text line3\ntext line4']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.