Python code that removes contiguous spaces into one

I don't always understand so make a note

import re

text = "I want to erase these ← half-width spaces and ← full-width spaces, but leave one."
text = re.sub('[  ]+', ' ', text)

print(text)
'I want to erase these ← half-width spaces and ← full-width spaces, but leave one.'

If you specify the space re.sub ('[(half-width space) (full-width space)] +',' (half-width space)', text) It's like that.

Recommended Posts

Python code that removes contiguous spaces into one
Python that merges a lot of excel into one excel
That Python code has no classes ...
[Python] Combine multiple Excel sheets into one
Combine python function arguments into one variable
Combine multiple python files into one python file
One liner that outputs multiplication tables in Python
[Python & Unix] Combine multiple PDF files into one.
[Automation] Convert Python code into an exe file
Automatically format Python code into PEP8-compliant code with Emacs
The one that displays the progress bar in Python
python character code
A script that combines your favorite python modules and binaries into one Lambda Layer
[Python] Algorithm-aware code
Let's write python code that parses go code and generates go code
The one that just translated the Py2app option into Japanese
One liner that outputs 1000000 digits of pi in Python
[Python] tkinter Code that is likely to be reused
[Python] pandas Code that is likely to be reused
I felt that I ported the Python code to C ++ 98.
A Python program that converts ical data into text
Weird Python error message ——Is that code really executed?
Macro (VBA) that converts the contents selected in the range in Excel into the list initialization code in Python.