How can I remove a trailing newline?

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

    Just use :

    line = line.rstrip("\n")
    

    or

    line = line.strip("\n")
    

    You don't need any of this complicated stuff

提交回复
热议问题