Substitute multiple whitespace with single whitespace in Python
A simple possibility (if you'd rather avoid REs) is
' '.join(mystring.split())
The split and join perform the task you're explicitly asking about -- plus, they also do the extra one that you don't talk about but is seen in your example, removing trailing spaces;-).
How to best replace multiple whitespaces by one (in python)?
You can use:
import re
a = re.sub(' +',' ',a)
Is there a simple way to remove multiple spaces in a string?
>>> import re
>>> re.sub(' +', ' ', 'The quick brown fox')
'The quick brown fox'
replace multiple whitespace with single space but keep new lines in regex match (python)
You can use code below:
import re
test = "This is a test. \n Blah blah."
print(re.sub(r"(?:(?!\n)\s)+", " ",test))
Output
This is a test.
Blah blah.
Replace multi-spacing in strings with single whitespace - Python
look at this
In [1]: str1 = "This is a foo bar sentence with crazy spaces that irritates my program "
In [2]: ' '.join(str1.split())
Out[2]: 'This is a foo bar sentence with crazy spaces that irritates my program'
The method split()
returns a list of all the words in the string, using str as the separator (splits on all whitespace if left unspecified)
replace a single whitespace without replacing multiple whitespaces (Python)
use regex and word boundary:
>>> s="Lorem/ipsum/dolor/sit amet consetetur"
>>> import re
>>> re.sub(r"\b \b","",s)
'Lorem/ipsum/dolor/sit ametconsetetur'
>>>
This technique also handles the more general case:
>>> s="Lorem/ipsum/dolor/sit amet consetetur adipisci velit"
>>> re.sub(r"\b \b","",s)
'Lorem/ipsum/dolor/sit ametconsetetur adipiscivelit'
for start & end spaces, you'll have to work slightly harder, but it's still doable:
>>> s=" Lorem/ipsum/dolor/sit amet consetetur adipisci velit "
>>> re.sub(r"(^|\b) (\b|$)","",s)
'Lorem/ipsum/dolor/sit ametconsetetur adipiscivelit'
Just for fun, a last variant: use re.split
with a multiple space separation, preserve the split char using a group, then join the strings again, removing the spaces only if the string has some non-space in it:
"".join([x if x.isspace() else x.replace(" ","") for x in re.split("( {2,})",s)])
(I suppose that this is slower because of list creation & join though)
How do I replace multiple spaces with just one character?
This pattern will replace any groups of whitespace with a single underscore
newstring = '_'.join(input1.split())
If you only want to replace spaces (not tab/newline/linefeed etc.) it's probably easier to use a regex
import re
newstring = re.sub(' +', '_', input1)
How do I replace multiple blank spaces occuring in a text file with a single space in python
Split string by default splits string at spaces and ignores multiple consecutive spaces:
with open("test.234.txt", 'r') as f:
a=f.read()
with open("test234(double space replaced by singleones)" ,"w+") as f:
for i in range(len(a)-1):
f.write(' '.join(a[i].split()))
Replace any number of white spaces with a single white space
You cannot use a regular expression in the replace()
method for strings, you have to use the re module:
import re
mystring = re.sub(r'\s+', ' ', mystring)
Note the r
prefix before the string literal, this makes sure that the backslash in your regular expressions is interpreted properly. It wouldn't actually make a difference here, but for different escape sequences it can cause serious problems. For example '\b'
is a backspace character but r'\b'
is a backslash followed by a 'b', which is used for matching word boundaries in regex.
Related Topics
What's the Difference Between "Update" and "Update_Idletasks"
Unicodeencodeerror: 'Latin-1' Codec Can't Encode Character
How to Use Python to Get the System Hostname
When to Use Which Fuzz Function to Compare 2 Strings
Python - Activate Conda Env Through Shell Script
Stratified Train/Test-Split in Scikit-Learn
Python: Dynamically Create Function at Runtime
How to Apply a Function on Every Row on a Dataframe
Why Might Python's 'From' Form of an Import Statement Bind a Module Name
Python - How to Check List Monotonicity
How to Represent an Infinite Number in Python
Find and Replace Values in Xml Using Python
Operation on Every Pair of Element in a List