用Python从字符串中剥离标点符号的最佳方法似乎有一种比以下更简单的方法:import string
s = "string. With. Punctuation?" # Sample string out = s.translate(string.maketrans("",""), string.punctuation)在那里吗?
4 回答
德玛西亚99
TA贡献1770条经验 获得超3个赞
import re s = "string. With. Punctuation?"s = re.sub(r'[^\w\s]','',s)
慕的地10843
TA贡献1785条经验 获得超8个赞
Python 2
import string s = "string. With. Punctuation?"table = string.maketrans("","")new_s = s.translate(table, string.punctuation) # Output: string without punctuation
Python 3
import string s = "string. With. Punctuation?"table = str.maketrans({key: None for key in string.punctuation})new_s = s.translate(table) # Output: string without punctuation
添加回答
举报
0/150
提交
取消