如何删除尾随换行符?

发布于 2024-07-08 18:37:27 字数 78 浏览 11 评论 0原文

如果字符串的最后一个字符是换行符,如何删除它?

"abc\n"  -->  "abc"

How can I remove the last character of a string if it is a newline?

"abc\n"  -->  "abc"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(28

爱你是孤单的心事 2024-07-15 18:37:27

尝试方法 rstrip() (参见文档 Python 2Python 3)

>>> 'test string\n'.rstrip()
'test string'

Python 的 rstrip() 方法默认情况下会去除所有种尾随空格,而不是像 Perl 中的 chomp

>>> 'test string \n \r\n\n\r \n\n'.rstrip()
'test string'

仅去除换行符:

>>> 'test string \n \r\n\n\r \n\n'.rstrip('\n')
'test string \n \r\n\n\r '

除了rstrip()之外,还有方法strip()lstrip()。 这是他们三个的例子:

>>> s = "   \n\r\n  \n  abc   def \n\r\n  \n  "
>>> s.strip()
'abc   def'
>>> s.lstrip()
'abc   def \n\r\n  \n  '
>>> s.rstrip()
'   \n\r\n  \n  abc   def'

Try the method rstrip() (see doc Python 2 and Python 3)

>>> 'test string\n'.rstrip()
'test string'

Python's rstrip() method strips all kinds of trailing whitespace by default, not just one newline as Perl does with chomp.

>>> 'test string \n \r\n\n\r \n\n'.rstrip()
'test string'

To strip only newlines:

>>> 'test string \n \r\n\n\r \n\n'.rstrip('\n')
'test string \n \r\n\n\r '

In addition to rstrip(), there are also the methods strip() and lstrip(). Here is an example with the three of them:

>>> s = "   \n\r\n  \n  abc   def \n\r\n  \n  "
>>> s.strip()
'abc   def'
>>> s.lstrip()
'abc   def \n\r\n  \n  '
>>> s.rstrip()
'   \n\r\n  \n  abc   def'
吃兔兔 2024-07-15 18:37:27

我想说,获取没有尾随换行符的行的“Pythonic”方法是 splitlines()。

>>> text = "line 1\nline 2\r\nline 3\nline 4"
>>> text.splitlines()
['line 1', 'line 2', 'line 3', 'line 4']

And I would say the "pythonic" way to get lines without trailing newline characters is splitlines().

>>> text = "line 1\nline 2\r\nline 3\nline 4"
>>> text.splitlines()
['line 1', 'line 2', 'line 3', 'line 4']
花开半夏魅人心 2024-07-15 18:37:27

去除行尾 (EOL) 字符的规范方法是使用字符串 rstrip() 方法删除任何尾随的 \r 或 \n。 以下是 Mac、Windows 和 Unix EOL 字符的示例。

>>> 'Mac EOL\r'.rstrip('\r\n')
'Mac EOL'
>>> 'Windows EOL\r\n'.rstrip('\r\n')
'Windows EOL'
>>> 'Unix EOL\n'.rstrip('\r\n')
'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 函数不同,这将删除字符串末尾的所有指定字符,而不仅仅是一个:

>>> "Hello\n\n\n".rstrip("\n")
"Hello"

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.

>>> 'Mac EOL\r'.rstrip('\r\n')
'Mac EOL'
>>> 'Windows EOL\r\n'.rstrip('\r\n')
'Windows EOL'
>>> 'Unix EOL\n'.rstrip('\r\n')
'Unix EOL'

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:

>>> "Hello\n\n\n".rstrip("\n")
"Hello"
惜醉颜 2024-07-15 18:37:27

请注意,rstrip 的行为与 Perl 的 chomp() 并不完全相同,因为它不修改字符串。 也就是说,在 Perl 中:

$x="a\n";

chomp $x

导致 $x"a"

但在 Python 中:

x="a\n"

x.rstrip()

表示 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:

$x="a\n";

chomp $x

results in $x being "a".

but in Python:

x="a\n"

x.rstrip()

will mean that the value of x is still "a\n". Even x=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.

半枫 2024-07-15 18:37:27

我可能会使用这样的东西:

import os
s = s.rstrip(os.linesep)

我认为 rstrip("\n") 的问题是您可能希望确保行分隔符是可移植的。 (据传一些过时的系统使用“\r\n”)。 另一个问题是 rstrip 会去除重复的空格。 希望 os.linesep 包含正确的字符。 以上对我有用。

I might use something like this:

import os
s = s.rstrip(os.linesep)

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 that rstrip will strip out repeated whitespace. Hopefully os.linesep will contain the right characters. the above works for me.

德意的啸 2024-07-15 18:37:27

您可以使用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.

帅气称霸 2024-07-15 18:37:27
s = s.rstrip()

将删除字符串 s 末尾的所有换行符。 需要进行赋值,因为 rstrip 返回一个新字符串而不是修改原始字符串。

s = s.rstrip()

will remove all newlines at the end of the string s. The assignment is needed because rstrip returns a new string instead of modifying the original string.

烙印 2024-07-15 18:37:27
"line 1\nline 2\r\n...".replace('\n', '').replace('\r', '')
>>> 'line 1line 2...'

或者你总是可以通过正则表达式变得更极客

"line 1\nline 2\r\n...".replace('\n', '').replace('\r', '')
>>> 'line 1line 2...'

or you could always get geekier with regexps

·深蓝 2024-07-15 18:37:27

这将完全复制 perl 的 chomp(数组上的负行为)的“\​​n”行终止符:(

def chomp(x):
    if x.endswith("\r\n"): return x[:-2]
    if x.endswith("\n") or x.endswith("\r"): return x[:-1]
    return x

注意:它不会修改字符串“就地”;它不会去除额外的尾随空格;考虑 \r\n)

This would replicate exactly perl's chomp (minus behavior on arrays) for "\n" line terminator:

def chomp(x):
    if x.endswith("\r\n"): return x[:-2]
    if x.endswith("\n") or x.endswith("\r"): return x[:-1]
    return x

(Note: it does not modify string 'in place'; it does not strip extra trailing whitespace; takes \r\n in account)

没有心的人 2024-07-15 18:37:27

你可以使用 strip:

line = line.strip()

demo:

>>> "\n\n hello world \n\n".strip()
'hello world'

you can use strip:

line = line.strip()

demo:

>>> "\n\n hello world \n\n".strip()
'hello world'
久而酒知 2024-07-15 18:37:27

rstrip 在很多层面上都没有做与 chomp 相同的事情。 阅读 http://perldoc.perl.org/functions/chomp.html 并看到chomp 确实非常复杂。

然而,我的主要观点是 chomp 最多删除 1 个行结尾,而 rstrip 将删除尽可能多的行结尾。

在这里您可以看到 rstrip 删除了所有换行符:

>>> 'foo\n\n'.rstrip(os.linesep)
'foo'

可以使用 re.sub 来完成更接近典型 Perl chomp 用法的近似,如下所示:

>>> re.sub(os.linesep + r'\Z','','foo\n\n')
'foo\n'

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:

>>> 'foo\n\n'.rstrip(os.linesep)
'foo'

A much closer approximation of typical Perl chomp usage can be accomplished with re.sub, like this:

>>> re.sub(os.linesep + r'\Z','','foo\n\n')
'foo\n'
小忆控 2024-07-15 18:37:27

小心 "foo".rstrip(os.linesep):这只会截断 Python 执行平台的换行符。 想象一下,您正在 Linux 下输入 Windows 文件的行,例如:

$ python
Python 2.7.1 (r271:86832, Mar 18 2011, 09:09:48) 
[GCC 4.5.0 20100604 [gcc-4_5-branch revision 160292]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, sys
>>> sys.platform
'linux2'
>>> "foo\r\n".rstrip(os.linesep)
'foo\r'
>>>

使用 "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:

$ python
Python 2.7.1 (r271:86832, Mar 18 2011, 09:09:48) 
[GCC 4.5.0 20100604 [gcc-4_5-branch revision 160292]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, sys
>>> sys.platform
'linux2'
>>> "foo\r\n".rstrip(os.linesep)
'foo\r'
>>>

Use "foo".rstrip("\r\n") instead, as Mike says above.

木槿暧夏七纪年 2024-07-15 18:37:27

Python 文档中的示例仅使用 line.strip ()

Perl 的 chomp 函数仅在字符串末尾确实存在换行序列时才将其删除。

如果 process 从概念上讲是我需要的函数,以便对此文件中的每一行执行有用的操作,那么我计划在 Python 中执行此操作,如下所示:

import os
sep_pos = -len(os.linesep)
with open("file.txt") as f:
    for line in f:
        if line[sep_pos:] == os.linesep:
            line = line[:sep_pos]
        process(line)

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:

import os
sep_pos = -len(os.linesep)
with open("file.txt") as f:
    for line in f:
        if line[sep_pos:] == os.linesep:
            line = line[:sep_pos]
        process(line)
淡水深流 2024-07-15 18:37:27
import re

r_unwanted = re.compile("[\n\t\r]")
r_unwanted.sub("", your_text)
import re

r_unwanted = re.compile("[\n\t\r]")
r_unwanted.sub("", your_text)
不美如何 2024-07-15 18:37:27

我不用 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.

烟花肆意 2024-07-15 18:37:27

如果你的问题是清理多行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'))

若有似无的小暗淡 2024-07-15 18:37:27

我发现通过迭代器获取 chomped 行很方便,这与从文件对象中获取未 chomped 行的方式类似。 您可以使用以下代码来执行此操作:

def chomped_lines(it):
    return map(operator.methodcaller('rstrip', '\r\n'), it)

示例用法:

with open("file.txt") as infile:
    for line in chomped_lines(infile):
        process(line)

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:

def chomped_lines(it):
    return map(operator.methodcaller('rstrip', '\r\n'), it)

Sample usage:

with open("file.txt") as infile:
    for line in chomped_lines(infile):
        process(line)
情话墙 2024-07-15 18:37:27

我正在从我之前在另一个答案的评论中发布的答案中冒出基于正则表达式的答案。 我认为使用 restr.rstrip 更清晰、更明确地解决这个问题。

>>> import re

如果您想删除一个或多个尾随换行符:

>>> re.sub(r'[\n\r]+

如果您想删除所有位置的换行符(不仅仅是尾随):

>>> re.sub(r'[\n\r]+', '', '\nx\r\n')
'x'

如果您只想删除 1-2 个尾随换行符(即 \r\n\r\n\n\r\r\r< /code>, \n\n)

>>> re.sub(r'[\n\r]{1,2}

我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者 \r\n\n 仅此而已。

>>> re.sub(r'(?:\r\n|\n)

?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):


如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者 \r\n\n 仅此而已。


?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):


如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者 \r\n\n 仅此而已。


?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者 \r\n\n 仅此而已。

?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\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'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者 \r\n\n 仅此而已。

?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者 \r\n\n 仅此而已。

?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者 \r\n\n 仅此而已。

?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\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'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者 \r\n\n 仅此而已。

?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者 \r\n\n 仅此而已。

?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者 \r\n\n 仅此而已。

?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\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'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者 \r\n\n 仅此而已。

?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者 \r\n\n 仅此而已。

?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者 \r\n\n 仅此而已。

?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\n', count=1) '\nx'

?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者 \r\n\n 仅此而已。

?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者 \r\n\n 仅此而已。

?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

我有一种感觉,大多数人在这里真正想要的是删除一个出现的尾随换行符,或者 \r\n\n 仅此而已。

?: 用于创建一个非捕获组。)

(顺便说一句,这不是 '...'.rstrip('\n ', '').rstrip('\r', '') 的作用可能对于偶然发现此线程的其他人来说不清楚, str.rstrip 会删除尽可能多的尾随字符。尽可能,因此像 foo\n\n\n 这样的字符串会导致 foo 的误报,而您可能希望在剥离单个换行符后保留其他换行符尾随一个。)

, '', '\nx\r\n') '\nx'

如果您想删除所有位置的换行符(不仅仅是尾随):

如果您只想删除 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 than str.rstrip.

>>> import re

If you want to remove one or more trailing newline chars:

>>> re.sub(r'[\n\r]+

If you want to remove newline chars everywhere (not just trailing):

>>> re.sub(r'[\n\r]+', '', '\nx\r\n')
'x'

If you want to remove only 1-2 trailing newline chars (i.e., \r, \n, \r\n, \n\r, \r\r, \n\n)

>>> re.sub(r'[\n\r]{1,2}

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.

>>> re.sub(r'(?:\r\n|\n)

(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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\n\n', count=1) '\nx\n' >>> re.sub(r'(?:\r\n|\n)

(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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n\r\n', count=1) '\nx\r\n' >>> re.sub(r'(?:\r\n|\n)

(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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n', count=1) '\nx' >>> re.sub(r'(?:\r\n|\n)

(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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\n', count=1) '\nx'

(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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n\r\n') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n\r') '\nx\r' >>> re.sub(r'[\n\r]{1,2}

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

, '', '\nx\r\n') '\nx'

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 like foo\n\n\n would result in a false positive of foo whereas you may have wanted to preserve the other newlines after stripping a single trailing one.)

若水微香 2024-07-15 18:37:27

特殊情况的解决方案:

如果换行符是最后一个字符(大多数文件输入都是这种情况),那么对于集合中的任何元素,您可以按如下方式索引:

foobar= foobar[:-1]

切出换行符。

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:

foobar= foobar[:-1]

to slice out your newline character.

凉墨 2024-07-15 18:37:27

看起来 Perl 的 chomp 没有完美的模拟。 特别是, rstrip 无法处理多字符换行符,例如 <代码>\r\n。 但是, splitlines 确实 正如此处指出的
按照我对另一个问题的回答,您可以结合加入splitlines 删除/替换字符串 s 中的所有换行符:

''.join(s.splitlines())

以下命令删除恰好一个尾随换行符(我相信就像 chomp 那样)。 将 True 作为 keepends 参数传递给分割线会保留分隔符。 然后,再次调用 splitlines 以删除最后“行”上的分隔符:

def chomp(s):
    if len(s):
        lines = s.splitlines(True)
        last = lines.pop()
        return ''.join(lines + last.splitlines())
    else:
        return ''

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:

''.join(s.splitlines())

The following removes exactly one trailing newline (as chomp would, I believe). Passing True as the keepends argument to splitlines retain the delimiters. Then, splitlines is called again to remove the delimiters on just the last "line":

def chomp(s):
    if len(s):
        lines = s.splitlines(True)
        last = lines.pop()
        return ''.join(lines + last.splitlines())
    else:
        return ''
桃扇骨 2024-07-15 18:37:27
s = '''Hello  World \t\n\r\tHi There'''
# import the module string   
import string
# use the method translate to convert 
s.translate({ord(c): None for c in string.whitespace}
>>'HelloWorldHiThere'

使用正则表达式

s = '''  Hello  World 
\t\n\r\tHi '''
print(re.sub(r"\s+", "", s), sep='')  # \s matches all white spaces
>HelloWorldHi

替换 \n,\t,\r

s.replace('\n', '').replace('\t','').replace('\r','')
>'  Hello  World Hi '

正则表达式

s = '''Hello  World \t\n\r\tHi There'''
regex = re.compile(r'[\n\r\t]')
regex.sub("", s)
>'Hello  World Hi There'

使用带有 Join 的

s = '''Hello  World \t\n\r\tHi There'''
' '.join(s.split())
>'Hello  World Hi There'
s = '''Hello  World \t\n\r\tHi There'''
# import the module string   
import string
# use the method translate to convert 
s.translate({ord(c): None for c in string.whitespace}
>>'HelloWorldHiThere'

With regex

s = '''  Hello  World 
\t\n\r\tHi '''
print(re.sub(r"\s+", "", s), sep='')  # \s matches all white spaces
>HelloWorldHi

Replace \n,\t,\r

s.replace('\n', '').replace('\t','').replace('\r','')
>'  Hello  World Hi '

With regex

s = '''Hello  World \t\n\r\tHi There'''
regex = re.compile(r'[\n\r\t]')
regex.sub("", s)
>'Hello  World Hi There'

with Join

s = '''Hello  World \t\n\r\tHi There'''
' '.join(s.split())
>'Hello  World Hi There'
满身野味 2024-07-15 18:37:27
>>> '   spacious   '.rstrip()
'   spacious'
>>> "AABAA".rstrip("A")
  'AAB'
>>> "ABBA".rstrip("AB") # both AB and BA are stripped
   ''
>>> "ABCABBA".rstrip("AB")
   'ABC'
>>> '   spacious   '.rstrip()
'   spacious'
>>> "AABAA".rstrip("A")
  'AAB'
>>> "ABBA".rstrip("AB") # both AB and BA are stripped
   ''
>>> "ABCABBA".rstrip("AB")
   'ABC'
断肠人 2024-07-15 18:37:27

只需使用 :

line = line.rstrip("\n")

line = line.strip("\n")

你不需要任何这些复杂的东西

Just use :

line = line.rstrip("\n")

or

line = line.strip("\n")

You don't need any of this complicated stuff

紫罗兰の梦幻 2024-07-15 18:37:27

我们通常会遇到三种类型的行结束符:\n\r\r\nre.sub 中相当简单的正则表达式,即r"\r?\n?$",能够捕获所有这些。

(我们必须捕获所有,对吗?)

import re

re.sub(r"\r?\n?$", "", the_text, 1)

通过最后一个参数,我们将替换的出现次数限制为一个,在某种程度上模仿了 chomp。 示例:

import re

text_1 = "hellothere\n\n\n"
text_2 = "hellothere\n\n\r"
text_3 = "hellothere\n\n\r\n"

a = re.sub(r"\r?\n?$", "", text_1, 1)
b = re.sub(r"\r?\n?$", "", text_2, 1)
c = re.sub(r"\r?\n?$", "", text_3, 1)

...其中 a == b == cTrue

There are three types of line endings that we normally encounter: \n, \r and \r\n. A rather simple regular expression in re.sub, namely r"\r?\n?$", is able to catch them all.

(And we gotta catch 'em all, am I right?)

import re

re.sub(r"\r?\n?$", "", the_text, 1)

With the last argument, we limit the number of occurences replaced to one, mimicking chomp to some extent. Example:

import re

text_1 = "hellothere\n\n\n"
text_2 = "hellothere\n\n\r"
text_3 = "hellothere\n\n\r\n"

a = re.sub(r"\r?\n?$", "", text_1, 1)
b = re.sub(r"\r?\n?$", "", text_2, 1)
c = re.sub(r"\r?\n?$", "", text_3, 1)

... where a == b == c is True.

相思碎 2024-07-15 18:37:27

如果您关心速度(假设您有一个很长的字符串列表)并且您知道换行符的性质,那么字符串切片实际上比 rstrip 更快。 一个小测试来说明这一点:

import time

loops = 50000000

def method1(loops=loops):
    test_string = 'num\n'
    t0 = time.time()
    for num in xrange(loops):
        out_sting = test_string[:-1]
    t1 = time.time()
    print('Method 1: ' + str(t1 - t0))

def method2(loops=loops):
    test_string = 'num\n'
    t0 = time.time()
    for num in xrange(loops):
        out_sting = test_string.rstrip()
    t1 = time.time()
    print('Method 2: ' + str(t1 - t0))

method1()
method2()

输出:

Method 1: 3.92700004578
Method 2: 6.73000001907

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:

import time

loops = 50000000

def method1(loops=loops):
    test_string = 'num\n'
    t0 = time.time()
    for num in xrange(loops):
        out_sting = test_string[:-1]
    t1 = time.time()
    print('Method 1: ' + str(t1 - t0))

def method2(loops=loops):
    test_string = 'num\n'
    t0 = time.time()
    for num in xrange(loops):
        out_sting = test_string.rstrip()
    t1 = time.time()
    print('Method 2: ' + str(t1 - t0))

method1()
method2()

Output:

Method 1: 3.92700004578
Method 2: 6.73000001907
脱离于你 2024-07-15 18:37:27

这适用于 Windows 和 Linux(如果您只寻找重新解决方案,则重新子有点昂贵)

import re 
if re.search("(\\r|)\\n$", line):
    line = re.sub("(\\r|)\\n$", "", line)


This will work both for windows and linux (bit expensive with re sub if you are looking for only re solution)

import re 
if re.search("(\\r|)\\n$", line):
    line = re.sub("(\\r|)\\n$", "", line)

蓦然回首 2024-07-15 18:37:27

一揽子:

line = line.rstrip('\r|\n')

A catch all:

line = line.rstrip('\r|\n')
水水月牙 2024-07-15 18:37:27

怎么样:

line = line.rstrip('\r*\n')

How about:

line = line.rstrip('\r*\n')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文