Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - How to clear spaces from a text

Tags:

python

string

In Python, I have a lot of strings, containing spaces. I would like to clear all spaces from the text, except if it is in quotation marks.

Example input:

This is "an example text" containing spaces.

And I want to get:

Thisis"an example text"containingspaces.

line.split() is not good, I think, because it clears all of spaces from the text.

What do you recommend?

like image 519
Tűzálló Földgolyó Avatar asked Dec 06 '22 07:12

Tűzálló Földgolyó


1 Answers

For the simple case that only " are used as quotes:

>>> import re
>>> s = 'This is "an example text" containing spaces.'
>>> re.sub(r' (?=(?:[^"]*"[^"]*")*[^"]*$)', "", s)
'Thisis"an example text"containingspaces.'

Explanation:

[ ]      # Match a space
(?=      # only if an even number of spaces follows --> lookahead
 (?:     # This is true when the following can be matched:
  [^"]*" # Any number of non-quote characters, then a quote, then
  [^"]*" # the same thing again to get an even number of quotes.
 )*      # Repeat zero or more times.
 [^"]*   # Match any remaining non-quote characters
 $       # and then the end of the string.
)        # End of lookahead.
like image 115
Tim Pietzcker Avatar answered Dec 28 '22 06:12

Tim Pietzcker