Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在行尾拆分文本:忽略内联\n_Python_Python 3.x_Split_Line - Fatal编程技术网

Python 在行尾拆分文本:忽略内联\n

Python 在行尾拆分文本:忽略内联\n,python,python-3.x,split,line,Python,Python 3.x,Split,Line,我有一些带有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 gfsdf

我有一些带有
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
保留在行内?


如果您有任何建议,我们将不胜感激。

您希望使用原始字符串。在字符串文字之前添加一个r前缀,如下所示:

SOURCE = r'''Insert text here\n'''
string = string.decode('string_escape')
这将为您转义换行符

若要稍后(可能在分割之后)取消对其进行扫描,请获取字符串并按如下方式对其进行解码:

SOURCE = r'''Insert text here\n'''
string = string.decode('string_escape')

您希望Python如何区分文本中的
\n
和行末的
\n
之间的差异?如果文本内容是从文件复制(复制+粘贴)的,只需在
r
前面加上字符串文字,使其成为原始字符串:
r''my string contents'
。这将把
\n
与实际换行分开。