Reputation: 1
I've the following string in python, example:
"Peter North / John West"
Note that there are two spaces before and after the forward slash.
What should I do such that I can clean it to become
"Peter North_John West"
I tried using regex but I am not exactly sure how. Should I use re.sub or pandas.replace?
Upvotes: 0
Views: 120
Reputation: 464
You can use
a = "Peter North / John West"
import re
a = re.sub(' +/ +','_',a)
Any number of spaces with slash followed by any number of slashes can be replaced by this pattern.
Upvotes: 1
Reputation: 29742
In case of varying number of white spaces before and after /
:
import re
re.sub("\s+/\s+", "_", "Peter North / John West")
# Peter North_John West
Upvotes: 0