![](/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.