0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rstripでsuffixを文字列から取り除こうとしたら失敗した話

0
Posted at

rstripで、指定したsuffixとマッチしたら削るみたいなことをしたかった。

rstrip.py
'012345_1.jpg'.rstrip('_1.jpg')  #期待値: 012345        結果: 012345
'012345_9.jpg'.rstrip('_1.jpg')  #期待値: 012345_9.jpg  結果: 012345_9
'012345_11.jpg'.rstrip('_1.jpg') #期待値: 012345_11.jpg 結果: 012345

小一時間悩んだ結果ドキュメントにちゃんと書いてあった。

The chars argument is not a suffix; rather, all combinations of its values are stripped
5. Built-in Types — Python v2.7 documentation

格好よく書けるかと思ったんだけど、結局replaceで対応した。

replace.py
'012345_1.jpg'.replace('_1.jpg', '')  #期待値: 012345        結果: 012345
'012345_9.jpg'.replace('_1.jpg', '')  #期待値: 012345_9.jpg  結果: 012345_9.jpg
'012345_11.jpg'.replace('_1.jpg', '') #期待値: 012345_11.jpg 結果: 012345_11.jpg
0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?