6
7

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.

Python空白行削除のワンライナー

Last updated at Posted at 2018-03-19

Python の組み込み関数の str.stritp() が空白文字の削除なのでそれをうまく適用すればよい。

'\n'.join(filter(lambda x: x.strip(), text.split('\n')))

抽出する行の余分な空白文字も削除したい場合

'\n'.join(filter(lambda x: x.strip(), map(lambda x: x.strip(), text.split('\n'))))
6
7
1

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
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?