我想用字符串“ PUNCTUATION”替换所有标点符号
。我有很多长字符串,所以我需要高效的代码。
例如我有这样的字符串
s = "Hello. World, Awesome! Really?"
我希望输出变成这样
replaced_s = "Hello PUNCTUATION World PUNCTUATION Awesome PUNCTUATION Really PUNCTUATION"
我认为我可以使用replace,但是会不会花费太长时间? 有什么解决办法吗?
答案 0 :(得分:1)
尝试string.punctuation
import string
s = "Hello. World, Awesome! Really?"
for c in string.punctuation:
s = s.replace(c,' PUNCTUATION ')
s
#'Hello PUNCTUATION World PUNCTUATION Awesome PUNCTUATION Really PUNCTUATION '
或使用正则表达式:
import re
s = "Hello. World, Awesome! Really?"
s = re.sub(r'[^\w\s]',' PUNCTUATION ',s)
re.sub(' +', ' ',s)
#'Hello PUNCTUATION World PUNCTUATION Awesome PUNCTUATION Really PUNCTUATION '