How can I remove a trailing newline?

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

    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.

提交回复
热议问题