我在python中有以下字符串,例如:
"Peter North / John West"
注意,正斜杠前后有两个空格。
我该怎么做才能把它洗干净
"Peter North_John West"
我试过用regex,但我不确定怎么用。 我应该使用re.sub还是pandas.replace?
前后空格数不同时 / :
/
import re re.sub("\s+/\s+", "_", "Peter North / John West") # Peter North_John West
你可以使用
a = "Peter North / John West" import re a = re.sub(' +/ +','_',a)
这个模式可以替换带有斜杠和任意斜杠的任意数量的空格。