[英]How do I replace punctuation in a string in Python?
我想用Python中的字符串中的“” 替换 (而不是删除 )所有标点字符。
是否有以下口味的效果?
text = text.translate(string.maketrans("",""), string.punctuation)
这个答案适用于Python 2,仅适用于ASCII字符串:
字符串模块包含两个可以帮助您的东西:标点符号列表和“maketrans”函数。 以下是如何使用它们:
import string
replace_punctuation = string.maketrans(string.punctuation, ' '*len(string.punctuation))
text = text.translate(replace_punctuation)
改进的解决方案来自Best方法,从Python中删除字符串中的标点符号
import string
import re
regex = re.compile('[%s]' % re.escape(string.punctuation))
out = regex.sub(' ', "This is, fortunately. A Test! string")
# out = 'This is fortunately A Test string'
有一个更强大的解决方案依赖于正则表达式排除,而不是通过广泛的标点符号列表包含。
import re
print(re.sub('[^\w\s]', '', 'This is, fortunately. A Test! string'))
#Output - 'This is fortunately A Test string'
正则表达式捕获任何不是字母数字或空白字符的东西
替换为''?
。
翻译所有内容之间有什么区别;
进入''并删除所有;
?
这是删除所有;
:
s = 'dsda;;dsd;sad'
table = string.maketrans('','')
string.translate(s, table, ';')
你可以用translate翻译。
以我的具体方式,我从标点符号列表中删除了“+”和“&”:
all_punctuations = string.punctuation
selected_punctuations = re.sub(r'(\&|\+)', "", all_punctuations)
print selected_punctuations
str = "he+llo* ithis& place% if you * here @@"
punctuation_regex = re.compile('[%s]' % re.escape(selected_punctuations))
punc_free = punctuation_regex.sub("", str)
print punc_free
结果:如果你在这里,他+ llo ithis&place
此解决方法适用于python 3:
import string
ex_str = 'SFDF-OIU .df !hello.dfasf sad - - d-f - sd'
#because len(string.punctuation) = 32
table = str.maketrans(string.punctuation,' '*32)
res = ex_str.translate(table)
# res = 'SFDF OIU df hello dfasf sad d f sd'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.