简体   繁体   中英

Split text at line end: ignoring inline \n

I have some text with START and END tags something like:

SOURCE = '''
Text with \n \n and some more # an so ..

other text to be ignored
START
docu \n this text \n I need includive the capital start and end
but do not split \n \n only split at the actuall end of the line
END

gfsdfgadgfg \n\n\n \n
5 635634
START
similar # to the above I need \n all of this in the split line
but do not split \n \n only split at the actuall end of the line
END


more text to ignore
'''

And hope to prase it to something like

parts_splitted_by_actual_end_of_line = {
'Part1_lines' : 
['START',
'docu \n this text \n I need includive the capital start and end',
'but do not split \n \n only split at the actuall end of the line',
'END'],

'Part1_lines' : 
['START',
'similar # to the above I need \n all of this in the split line',
'but do not split \n \n only split at the actuall end of the line',
'END'],
}

I can find the START and END tags with string find and extract the text between.

But I'm completely stuck to split the lines keeping the \\n within the line ?

Any suggestion would be really appreciated.

You want to use a raw string. Add ar prefix before your string literal like this:

SOURCE = r'''Insert text here\n'''

This will do the escaping of your newline character for you.

To unescape it later afterwards (probably after your split), take the string and decode it like this:

string = string.decode('string_escape')

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.

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