![](/img/trans.png)
[英]Python split('\n') breaks a row midway instead of after end of line
[英]Split text at line end: ignoring inline \n
我有一些帶有START
和END
標簽的文本,例如:
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
'''
並希望將其添加到類似
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'],
}
我可以使用字符串查找找到START
和END
標簽,並提取其間的文本。
但是我完全被束縛在將\\n
保持在行內的情況下?
任何建議將不勝感激。
您要使用原始字符串。 在字符串文字之前添加ar前綴,如下所示:
SOURCE = r'''Insert text here\n'''
這將為您完成換行符的轉義。
要在以后(可能是在拆分之后)取消轉義,請采用以下字符串並對其進行解碼:
string = string.decode('string_escape')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.