Suppose this string:
The fox jumped over the log.
Turning into:
Because @pythonlarry asked here are the missing generator based versions
The groupby join is easy. Groupby will group elements consecutive with same key. And return pairs of keys and list of elements for each group. So when the key is an space an space is returne else the entire group.
from itertools import groupby
def group_join(string):
return ''.join(' ' if chr==' ' else ''.join(times) for chr,times in groupby(string))
The group by variant is simple but very slow. So now for the generator variant. Here we consume an iterator, the string, and yield all chars except chars that follow an char.
def generator_join_generator(string):
last=False
for c in string:
if c==' ':
if not last:
last=True
yield ' '
else:
last=False
yield c
def generator_join(string):
return ''.join(generator_join_generator(string))
So i meassured the timings with some other lorem ipsum.
With Hello and World separated by 64KB of spaces
Not forget the original sentence
Interesting here for nearly space only strings group join is not that worse Timing showing always median from seven runs of a thousand times each.
Similar to the previous solutions, but more specific: replace two or more spaces with one:
>>> import re
>>> s = "The fox jumped over the log."
>>> re.sub('\s{2,}', ' ', s)
'The fox jumped over the log.'
A simple soultion
>>> import re
>>> s="The fox jumped over the log."
>>> print re.sub('\s+',' ', s)
The fox jumped over the log.
import re
string = re.sub('[ \t\n]+', ' ', 'The quick brown \n\n \t fox')
This will remove all the tabs, new lines and multiple white spaces with single white space.
To remove white space, considering leading, trailing and extra white space in between words, use:
(?<=\s) +|^ +(?=\s)| (?= +[\n\0])
The first or
deals with leading white space, the second or
deals with start of string leading white space, and the last one deals with trailing white space.
For proof of use, this link will provide you with a test.
https://regex101.com/r/meBYli/4
This is to be used with the re.split function.
In some cases it's desirable to replace consecutive occurrences of every whitespace character with a single instance of that character. You'd use a regular expression with backreferences to do that.
(\s)\1{1,}
matches any whitespace character, followed by one or more occurrences of that character. Now, all you need to do is specify the first group (\1
) as the replacement for the match.
Wrapping this in a function:
import re
def normalize_whitespace(string):
return re.sub(r'(\s)\1{1,}', r'\1', string)
>>> normalize_whitespace('The fox jumped over the log.')
'The fox jumped over the log.'
>>> normalize_whitespace('First line\t\t\t \n\n\nSecond line')
'First line\t \nSecond line'