Python 拆分后,我的readline()的空字符串是什么

Python 拆分后,我的readline()的空字符串是什么,python,regex,string,python-2.7,Python,Regex,String,Python 2.7,我从输入文件中读取行,并将每行拆分为列表。然而,我遇到了以下让我困惑的情况 这是我的代码: with open("filename") as in_file: for line in in_file: print re.split(r'([\s,:()\[\]=|/\\{}\'\"<>]+)', line) 打开(“文件名”),如在\u文件中所示: 对于\u文件中的行: 打印重新拆分(r'([\s,:()\[\]=\\\/\\{}'\“]+),第行) 这是

我从输入文件中读取行,并将每行拆分为列表。然而,我遇到了以下让我困惑的情况

这是我的代码:

with open("filename") as in_file:
    for line in in_file:
        print re.split(r'([\s,:()\[\]=|/\\{}\'\"<>]+)', line)
打开(“文件名”),如在\u文件中所示:
对于\u文件中的行:
打印重新拆分(r'([\s,:()\[\]=\\\/\\{}'\“]+),第行)
这是我的输入文件的演示:

PREREQUISITES

    CUDA 7.0 and a GPU of compute capability 3.0 or higher are required.


    Extract the cuDNN archive to a directory of your choice, referred to below as <installpath>.
    Then follow the platform-specific instructions as follows.
先决条件
需要CUDA 7.0和计算能力为3.0或更高的GPU。
将cuDNN归档文件解压缩到您选择的目录中,以下称为。
然后按照以下特定于平台的说明进行操作。
这是我得到的输出结果:

['PREREQUISITES', '\n', '']
['', '\n', '']
['', '    ', 'CUDA', ' ', '7.0', ' ', 'and', ' ', 'a', ' ', 'GPU', ' ', 'of', ' ', 'compute', ' ', 'capability', ' ', '3.0', ' ', 'or', ' ', 'higher', ' ', 'are', ' ', 'required.', '\n', '']
['', '\n', '']
['', '\n', '']
['', '    ', 'Extract', ' ', 'the', ' ', 'cuDNN', ' ', 'archive', ' ', 'to', ' ', 'a', ' ', 'directory', ' ', 'of', ' ', 'your', ' ', 'choice', ', ', 'referred', ' ', 'to', ' ', 'below', ' ', 'as', ' <', 'installpath', '>', '.', '\n', '']
['', '    ', 'Then', ' ', 'follow', ' ', 'the', ' ', 'platform-specific', ' ', 'instructions', ' ', 'as', ' ', 'follows.', '\n', '']
[“先决条件”、“\n”、”]
['',\n'','']
['','CUDA','','','',7.0',''和','',a','',GPU','',of','','',compute','',capability','','',3.0','',或','',更高','','',是','',必需的','\n',']
['',\n'','']
['',\n'','']
[“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”
['','然后','',''跟随','','',''平台特定','',''说明',''作为',''跟随','\n','']
我的问题是:

Q1:在每行末尾,除了字符
\n
,还有一个空元素
'
。那是什么

问题2:第一行的esides,所有其他行都以这个空元素开始

编辑:

添加问题Q3:我希望在结果中保留分隔符,如
'
'\n'
,但不保留此空元素
'
。有什么方法可以这样做吗

对问题Q1-2的答复:


对问题Q3的回答:。

空字符串表示
'\n'
已匹配为行中的最后一个字符,且其后没有更多数据。即:

>>> re.split(r'([\s]+)', 'hello world\n')
['hello', ' ', 'world', '\n', '']
应产生与以下不同的结果:

>>> re.split(r'([\s]+)', 'hello world')
['hello', ' ', 'world']
可以在拆分线之前先拆分线:

>>> re.split(r'([\s]+)', 'hello world\n'.strip())
['hello', ' ', 'world']
或者反转正则表达式,改用
findall
findall
的工作方式不同,因为它不会在匹配文本之间生成序列

>>> re.findall(r'([^\s]+)', 'hello world\n')
['hello', 'world']

但是为什么第一行不具有这样的空字符串?匹配(n)和与(s)的分裂,为什么会得到“<代码>”、“n”、“<”/“代码”,这是正常的,因为第一行不是以\n开头的吗?“第二行上的空白”和“向前”,是因为缩进,这是由totoi匹配的匹配文本(在<代码>分隔符< /代码>中的分隔符)。例如
'
'\n'
但不是这个空元素
'
。有什么方法可以做到这一点吗?