4
2

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 3 years have passed since last update.

連続するスペースを削除して1つにするpythonのコード

Last updated at Posted at 2020-02-21

いつもわからなくなるのでメモ

import re

text = "こういった、     ←半角スペースや   ←全角スペース消したいけど、一個は残す。"
text = re.sub('[  ]+', ' ', text)

print(text)
'こういった、 ←半角スペースや ←全角スペース消したいけど、一個は残す。'

スペースのところを明記すると
re.sub('[(半角スペース)(全角スペース)]+','(半角スペース)',text)
という感じです。

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?