Python:删除字符串中的特殊字符
我使用python编写了以下字符串,例如:Python:删除字符串中的特殊字符,python,regex,pandas,replace,Python,Regex,Pandas,Replace,我使用python编写了以下字符串,例如: "Peter North / John West" 请注意,正斜杠前后有两个空格 我该怎么做才能把它清理干净 "Peter North_John West" 我试过使用正则表达式,但我不确定如何使用。 我应该使用re.sub还是pandas.replace?如果在/前后出现不同数量的空格: import re re.sub("\s+/\s+", "_", "Peter North / John West") # Peter North_J
"Peter North / John West"
请注意,正斜杠前后有两个空格
我该怎么做才能把它清理干净
"Peter North_John West"
我试过使用正则表达式,但我不确定如何使用。
我应该使用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)
此模式可以替换任意数量的空格(斜杠后跟任意数量的斜杠) “Peter North/John West”怎么办?替换(“/”,“”)?总是两个空格,然后是正斜杠,然后是两个空格吗?还是担心是正斜杠?