0

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?

2
  • 11
    What about "Peter North / John West".replace(" / ", "_") ? Commented Feb 26, 2019 at 6:56
  • 1
    Is it always two spaces, then a forward slash, then 2 spaces? Or is the concern the forward slash? Commented Feb 26, 2019 at 6:58

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.