How can I remove a trailing newline?

前端 未结 28 3540
感动是毒
感动是毒 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:52

    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.

提交回复
热议问题