如何删除尾随换行符?
如果字符串的最后一个字符是换行符,如何删除它?
"abc\n" --> "abc"
How can I remove the last character of a string if it is a newline?
"abc\n" --> "abc"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(28)
尝试方法
rstrip()
(参见文档 Python 2 和 Python 3)Python 的
rstrip()
方法默认情况下会去除所有种尾随空格,而不是像 Perl 中的chomp
。仅去除换行符:
除了
rstrip()
之外,还有方法strip()
和lstrip()
。 这是他们三个的例子:Try the method
rstrip()
(see doc Python 2 and Python 3)Python's
rstrip()
method strips all kinds of trailing whitespace by default, not just one newline as Perl does withchomp
.To strip only newlines:
In addition to
rstrip()
, there are also the methodsstrip()
andlstrip()
. Here is an example with the three of them:我想说,获取没有尾随换行符的行的“Pythonic”方法是 splitlines()。
And I would say the "pythonic" way to get lines without trailing newline characters is splitlines().
去除行尾 (EOL) 字符的规范方法是使用字符串 rstrip() 方法删除任何尾随的 \r 或 \n。 以下是 Mac、Windows 和 Unix EOL 字符的示例。
使用“\r\n”作为 rstrip 的参数意味着它将删除“\r”或“\n”的任何尾随组合。 这就是为什么它在上述所有三种情况下都有效。
在极少数情况下,这种细微差别很重要。 例如,我曾经必须处理一个包含 HL7 消息的文本文件。 HL7 标准要求尾随“\r”作为其 EOL 字符。 我使用此消息的 Windows 计算机已附加其自己的“\r\n”EOL 字符。 因此,每行的结尾看起来像“\r\r\n”。 使用 rstrip('\r\n') 会删除整个 '\r\r\n',这不是我想要的。 在这种情况下,我只是简单地切掉最后两个字符。
请注意,与 Perl 的 chomp 函数不同,这将删除字符串末尾的所有指定字符,而不仅仅是一个:
The canonical way to strip end-of-line (EOL) characters is to use the string rstrip() method removing any trailing \r or \n. Here are examples for Mac, Windows, and Unix EOL characters.
Using '\r\n' as the parameter to rstrip means that it will strip out any trailing combination of '\r' or '\n'. That's why it works in all three cases above.
This nuance matters in rare cases. For example, I once had to process a text file which contained an HL7 message. The HL7 standard requires a trailing '\r' as its EOL character. The Windows machine on which I was using this message had appended its own '\r\n' EOL character. Therefore, the end of each line looked like '\r\r\n'. Using rstrip('\r\n') would have taken off the entire '\r\r\n' which is not what I wanted. In that case, I simply sliced off the last two characters instead.
Note that unlike Perl's
chomp
function, this will strip all specified characters at the end of the string, not just one:请注意,rstrip 的行为与 Perl 的 chomp() 并不完全相同,因为它不修改字符串。 也就是说,在 Perl 中:
导致
$x
为"a"
。但在 Python 中:
表示
x
的值为 still"a\n"
。 即使 x=x.rstrip() 也并不总是给出相同的结果,因为它从字符串末尾删除所有空格,而不仅仅是一个换行符。Note that rstrip doesn't act exactly like Perl's chomp() because it doesn't modify the string. That is, in Perl:
results in
$x
being"a"
.but in Python:
will mean that the value of
x
is still"a\n"
. Evenx=x.rstrip()
doesn't always give the same result, as it strips all whitespace from the end of the string, not just one newline at most.我可能会使用这样的东西:
我认为 rstrip("\n") 的问题是您可能希望确保行分隔符是可移植的。 (据传一些过时的系统使用
“\r\n”
)。 另一个问题是 rstrip 会去除重复的空格。 希望 os.linesep 包含正确的字符。 以上对我有用。I might use something like this:
I think the problem with
rstrip("\n")
is that you'll probably want to make sure the line separator is portable. (some antiquated systems are rumored to use"\r\n"
). The other gotcha is thatrstrip
will strip out repeated whitespace. Hopefullyos.linesep
will contain the right characters. the above works for me.您可以使用
line = line.rstrip('\n')
。 这将从字符串末尾去除所有换行符,而不仅仅是一个。You may use
line = line.rstrip('\n')
. This will strip all newlines from the end of the string, not just one.将删除字符串
s
末尾的所有换行符。 需要进行赋值,因为 rstrip 返回一个新字符串而不是修改原始字符串。will remove all newlines at the end of the string
s
. The assignment is needed becauserstrip
returns a new string instead of modifying the original string.或者你总是可以通过正则表达式变得更极客
or you could always get geekier with regexps
这将完全复制 perl 的 chomp(数组上的负行为)的“\n”行终止符:(
注意:它不会修改字符串“就地”;它不会去除额外的尾随空格;考虑 \r\n)
This would replicate exactly perl's chomp (minus behavior on arrays) for "\n" line terminator:
(Note: it does not modify string 'in place'; it does not strip extra trailing whitespace; takes \r\n in account)
你可以使用 strip:
demo:
you can use strip:
demo:
rstrip 在很多层面上都没有做与 chomp 相同的事情。 阅读 http://perldoc.perl.org/functions/chomp.html 并看到chomp 确实非常复杂。
然而,我的主要观点是 chomp 最多删除 1 个行结尾,而 rstrip 将删除尽可能多的行结尾。
在这里您可以看到 rstrip 删除了所有换行符:
可以使用 re.sub 来完成更接近典型 Perl chomp 用法的近似,如下所示:
rstrip doesn't do the same thing as chomp, on so many levels. Read http://perldoc.perl.org/functions/chomp.html and see that chomp is very complex indeed.
However, my main point is that chomp removes at most 1 line ending, whereas rstrip will remove as many as it can.
Here you can see rstrip removing all the newlines:
A much closer approximation of typical Perl chomp usage can be accomplished with re.sub, like this:
小心
"foo".rstrip(os.linesep)
:这只会截断 Python 执行平台的换行符。 想象一下,您正在 Linux 下输入 Windows 文件的行,例如:使用
"foo".rstrip("\r\n")
代替,如 Mike 上面所说。Careful with
"foo".rstrip(os.linesep)
: That will only chomp the newline characters for the platform where your Python is being executed. Imagine you're chimping the lines of a Windows file under Linux, for instance:Use
"foo".rstrip("\r\n")
instead, as Mike says above.Python 文档中的示例仅使用
line.strip ()
。Perl 的 chomp 函数仅在字符串末尾确实存在换行序列时才将其删除。
如果
process
从概念上讲是我需要的函数,以便对此文件中的每一行执行有用的操作,那么我计划在 Python 中执行此操作,如下所示:An example in Python's documentation simply uses
line.strip()
.Perl's
chomp
function removes one linebreak sequence from the end of a string only if it's actually there.Here is how I plan to do that in Python, if
process
is conceptually the function that I need in order to do something useful to each line from this file:我不用 Python 编程,但我遇到了 python.org 上的常见问题解答,提倡使用 Python 2.2 或更高版本的 S.rstrip("\r\n")。
I don't program in Python, but I came across an FAQ at python.org advocating S.rstrip("\r\n") for python 2.2 or later.
如果你的问题是清理多行str对象(oldstr)中的所有换行符,你可以根据分隔符'\n'将其拆分成一个列表,然后将这个列表加入到一个新的str(newstr)中。
newstr = "".join(oldstr.split('\n'))
If your question is to clean up all the line breaks in a multiple line str object (oldstr), you can split it into a list according to the delimiter '\n' and then join this list into a new str(newstr).
newstr = "".join(oldstr.split('\n'))
我发现通过迭代器获取 chomped 行很方便,这与从文件对象中获取未 chomped 行的方式类似。 您可以使用以下代码来执行此操作:
示例用法:
I find it convenient to have be able to get the chomped lines via in iterator, parallel to the way you can get the un-chomped lines from a file object. You can do so with the following code:
Sample usage:
我正在从我之前在另一个答案的评论中发布的答案中冒出基于正则表达式的答案。 我认为使用
re
比str.rstrip
更清晰、更明确地解决这个问题。如果您想删除一个或多个尾随换行符:
如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\n\n', count=1) '\nx\n' >>> re.sub(r'(?:\r\n|\n)'...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}'...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}'...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n\r\n', count=1) '\nx\r\n' >>> re.sub(r'(?:\r\n|\n)'...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}'...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}'...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n', count=1) '\nx' >>> re.sub(r'(?:\r\n|\n)'...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}'...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}'...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\n', count=1) '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}'...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}'...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
, '', '\nx\r\n') '\nx''...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)如果您想删除所有位置的换行符(不仅仅是尾随):
如果您只想删除 1-2 个尾随换行符(即
\r
、\n
、\r\n
、\n\r
、\r\r< /code>,
\n\n
)我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者
\r\n
或\n
仅此而已。(
?:
用于创建一个非捕获组。)(顺便说一句,这不是
'...'.rstrip('\n ', '').rstrip('\r', '')
的作用可能对于偶然发现此线程的其他人来说不清楚,str.rstrip
会删除尽可能多的尾随字符。尽可能,因此像foo\n\n\n
这样的字符串会导致foo
的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)I'm bubbling up my regular expression based answer from one I posted earlier in the comments of another answer. I think using
re
is a clearer more explicit solution to this problem thanstr.rstrip
.If you want to remove one or more trailing newline chars:
If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\n\n', count=1) '\nx\n' >>> re.sub(r'(?:\r\n|\n)'...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}'...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}'...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n\r\n', count=1) '\nx\r\n' >>> re.sub(r'(?:\r\n|\n)'...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}'...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}'...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n', count=1) '\nx' >>> re.sub(r'(?:\r\n|\n)'...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}'...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}'...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\n', count=1) '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}'...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}'...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
, '', '\nx\r\n') '\nx''...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)If you want to remove newline chars everywhere (not just trailing):
If you want to remove only 1-2 trailing newline chars (i.e.,
\r
,\n
,\r\n
,\n\r
,\r\r
,\n\n
)I have a feeling what most people really want here, is to remove just one occurrence of a trailing newline character, either
\r\n
or\n
and nothing more.(The
?:
is to create a non-capturing group.)(By the way this is not what
'...'.rstrip('\n', '').rstrip('\r', '')
does which may not be clear to others stumbling upon this thread.str.rstrip
strips as many of the trailing characters as possible, so a string likefoo\n\n\n
would result in a false positive offoo
whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)特殊情况的解决方案:
如果换行符是最后一个字符(大多数文件输入都是这种情况),那么对于集合中的任何元素,您可以按如下方式索引:
切出换行符。
workaround solution for special case:
if the newline character is the last character (as is the case with most file inputs), then for any element in the collection you can index as follows:
to slice out your newline character.
看起来 Perl 的 chomp 没有完美的模拟。 特别是, rstrip 无法处理多字符换行符,例如 <代码>\r\n。 但是, splitlines 确实 正如此处指出的。
按照我对另一个问题的回答,您可以结合加入 和 splitlines 删除/替换字符串
s
中的所有换行符:以下命令删除恰好一个尾随换行符(我相信就像 chomp 那样)。 将
True
作为keepends
参数传递给分割线会保留分隔符。 然后,再次调用 splitlines 以删除最后“行”上的分隔符:It looks like there is not a perfect analog for perl's chomp. In particular, rstrip cannot handle multi-character newline delimiters like
\r\n
. However, splitlines does as pointed out here.Following my answer on a different question, you can combine join and splitlines to remove/replace all newlines from a string
s
:The following removes exactly one trailing newline (as chomp would, I believe). Passing
True
as thekeepends
argument to splitlines retain the delimiters. Then, splitlines is called again to remove the delimiters on just the last "line":使用正则表达式
替换 \n,\t,\r
正则表达式
使用带有 Join 的
With regex
Replace \n,\t,\r
With regex
with Join
只需使用 :
或
你不需要任何这些复杂的东西
Just use :
or
You don't need any of this complicated stuff
我们通常会遇到三种类型的行结束符:
\n
、\r
和\r\n
。re.sub
中相当简单的正则表达式,即r"\r?\n?$"
,能够捕获所有这些。(我们必须捕获所有,对吗?)
通过最后一个参数,我们将替换的出现次数限制为一个,在某种程度上模仿了 chomp。 示例:
...其中
a == b == c
为True
。There are three types of line endings that we normally encounter:
\n
,\r
and\r\n
. A rather simple regular expression inre.sub
, namelyr"\r?\n?$"
, is able to catch them all.(And we gotta catch 'em all, am I right?)
With the last argument, we limit the number of occurences replaced to one, mimicking chomp to some extent. Example:
... where
a == b == c
isTrue
.如果您关心速度(假设您有一个很长的字符串列表)并且您知道换行符的性质,那么字符串切片实际上比 rstrip 更快。 一个小测试来说明这一点:
输出:
If you are concerned about speed (say you have a looong list of strings) and you know the nature of the newline char, string slicing is actually faster than rstrip. A little test to illustrate this:
Output:
这适用于 Windows 和 Linux(如果您只寻找重新解决方案,则重新子有点昂贵)
This will work both for windows and linux (bit expensive with re sub if you are looking for only re solution)
一揽子:
A catch all:
怎么样:
How about: