[英]How do I replace punctuation in a string in Python?
I would like to replace (and not remove ) all punctuation characters by " " in a string in Python. 我想用Python中的字符串中的“” 替换 (而不是删除 )所有标点字符。
Is there something efficient of the following flavour? 是否有以下口味的效果?
text = text.translate(string.maketrans("",""), string.punctuation)
This answer is for Python 2 and will only work for ASCII strings: 这个答案适用于Python 2,仅适用于ASCII字符串:
The string module contains two things that will help you: a list of punctuation characters and the "maketrans" function. 字符串模块包含两个可以帮助您的东西:标点符号列表和“maketrans”函数。 Here is how you can use them: 以下是如何使用它们:
import string
replace_punctuation = string.maketrans(string.punctuation, ' '*len(string.punctuation))
text = text.translate(replace_punctuation)
Modified solution from Best way to strip punctuation from a string in Python 改进的解决方案来自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'
There is a more robust solution which relies on a regex exclusion rather than inclusion through an extensive list of punctuation characters. 有一个更强大的解决方案依赖于正则表达式排除,而不是通过广泛的标点符号列表包含。
import re
print(re.sub('[^\w\s]', '', 'This is, fortunately. A Test! string'))
#Output - 'This is fortunately A Test string'
The regex catches anything which is not an alpha-numeric or whitespace character 正则表达式捕获任何不是字母数字或空白字符的东西
Replace by ''?
替换为''?
. 。
What's the difference between translating all ;
翻译所有内容之间有什么区别;
into '' and remove all ;
进入''并删除所有;
? ?
Here is to remove all ;
这是删除所有;
: :
s = 'dsda;;dsd;sad'
table = string.maketrans('','')
string.translate(s, table, ';')
And you can do your replacement with translate. 你可以用translate翻译。
In my specific way, I removed "+" and "&" from the punctuation list: 以我的具体方式,我从标点符号列表中删除了“+”和“&”:
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
Result: he+llo ithis& place if you here 结果:如果你在这里,他+ llo ithis&place
This workaround works in python 3: 此解决方法适用于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.