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)
打开了文件。在打开的manual之后:因此,简而言之,请尝试使用
'rU'
打开文件,看看\r
是否消失:输出中的
\r
是否消失?在当然,您的代码看起来相当笨拙,但其他人已经对这一部分发表了评论。在
您可能以二进制模式(
open(filename, "rb")
或类似的方式)打开文件。如果您正在处理文本文件,请不要这样做。在请改用
open(filename)
。现在Python将自动将所有换行规范化为\n
,而不管当前平台是什么。在还有,为什么不简单地
patternFixes = r'\s*Fixes:\s*'
?为什么所有的+
es?在然后,你做了很多不必要的事情,比如一遍又一遍地重新编译正则表达式。在
因此,我的建议(它的作用与代码相同(加上文件处理):
没有'\r'
^{pr2}$又没有'\r'
你能提供更多关于如何复制的细节吗?在
编辑也不能用代码重新生成
相关问题 更多 >
编程相关推荐