[英]Python: Remove special character in string
我在python中有以下字符串,例如:
"Peter North / John West"
請注意,在正斜杠前后有兩個空格。
我應該怎么做才能清潔它成為
"Peter North_John West"
我嘗試使用正則表達式,但我不確定如何使用。 我應該使用re.sub還是pandas.replace?
您可以使用
a = "Peter North / John West"
import re
a = re.sub(' +/ +','_',a)
此模式可以替換任意數量的帶斜杠的空格以及任意數量的斜杠。
如果在/
之前和之后的空白數量不同,
import re
re.sub("\s+/\s+", "_", "Peter North / John West")
# Peter North_John West
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.