How can I remove a trailing newline?

前端 未结 28 3486
感动是毒
感动是毒 2020-11-21 23:27

What is the Python equivalent of Perl\'s chomp function, which removes the last character of a string if it is a newline?

28条回答
  •  有刺的猬
    2020-11-21 23:55

    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]+$', '', '\nx\r\n')
    '\nx'
    

    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}$', '', '\nx\r\n\r\n')
    '\nx\r'
    >>> re.sub(r'[\n\r]{1,2}$', '', '\nx\r\n\r')
    '\nx\r'
    >>> re.sub(r'[\n\r]{1,2}$', '', '\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.

    >>> re.sub(r'(?:\r\n|\n)$', '', '\nx\n\n', count=1)
    '\nx\n'
    >>> re.sub(r'(?:\r\n|\n)$', '', '\nx\r\n\r\n', count=1)
    '\nx\r\n'
    >>> re.sub(r'(?:\r\n|\n)$', '', '\nx\r\n', count=1)
    '\nx'
    >>> re.sub(r'(?:\r\n|\n)$', '', '\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.)

提交回复
热议问题