文件读取和regex Python

2024-06-17 12:53:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我读了一个文件,其中有一行:Fixes: Saurabh Likes python

我想删除上面一行的Fixes:部分。我用的是正则表达式 但下面的代码片段返回的输出如下所示

Saurabh Likes python\r

我想知道\r是从哪里来的。我尝试了所有的去除它的方法,比如rstrip()lstrip(),等等,但是没有任何效果。有谁能给我建议一下摆脱\r的方法吗。在

^{pr2}$

提前谢谢! -苏拉布


Tags: 文件方法代码建议fixeslikes效果lstrip
3条回答

建议删除\r

我想您已经用open(filename)打开了文件。在打开的manual之后:

If mode is omitted, it defaults to 'r'. ... In addition to the standard fopen() values mode may be 'U' or 'rU'. Python is usually built with universal newlines support; supplying 'U' opens the file as a text file, but lines may be terminated by any of the following: the Unix end-of-line convention '\n', the Macintosh convention '\r', or the Windows convention '\r\n'. All of these external representations are seen as '\n' by the Python program.

因此,简而言之,请尝试使用'rU'打开文件,看看\r是否消失:

with open(filename, "rU") as f:
    # do your stuff here.
    ...

输出中的\r是否消失?在

当然,您的代码看起来相当笨拙,但其他人已经对这一部分发表了评论。在

您可能以二进制模式(open(filename, "rb")或类似的方式)打开文件。如果您正在处理文本文件,请不要这样做。在

请改用open(filename)。现在Python将自动将所有换行规范化为\n,而不管当前平台是什么。在

还有,为什么不简单地patternFixes = r'\s*Fixes:\s*'?为什么所有的+es?在

然后,你做了很多不必要的事情,比如一遍又一遍地重新编译正则表达式。在

因此,我的建议(它的作用与代码相同(加上文件处理):

r = re.compile(r'\s*Fixes:\s*')
with open(filename) as infile:
    relevantInfo = "".join(r.sub("", line) for line in infile if "Fixes:" in line)
>>> import re
>>> re.sub('Fixes:\s*', '', 'Fixes: Saurabh Likes python')
'Saurabh Likes python'

没有'\r'

^{pr2}$

又没有'\r'

你能提供更多关于如何复制的细节吗?在

编辑也不能用代码重新生成

>>> line = 'Fixes: Saurabh Likes python'
>>> patternFixes ='\s*'+'Fixes'+':'+'\s*'
>>> matchFixes= re.search(patternFixes,line, re.IGNORECASE)
>>> if matchFixes:
...     patternCompiled = re.compile(patternFixes)
...     line=patternCompiled.sub("", line)
...     print line
...     line=line.lstrip()
...     print line
... 
Saurabh Likes python
Saurabh Likes python
>>> 

相关问题 更多 >