Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

pythonでchomp的な事をやりたいときは「rstrip」

Pythonのrstripメソッドは末尾の改行を消します。
RubyのString#chompですね。

for i in open('./example.yaml'):
     print(i.rstrip())

#foo:
#  bar:
#    - baz
#    - 3

一応、非破壊メソッドのようです

>>> bar = "string\n\n"
>>> bar
'string\n\n'
>>> bar.rstrip()
'string'
>>> bar
'string\n\n'