Python3 中高效去除字符串标点符号的方法
本文介绍在Python3环境下去除字符串标点符号的有效方法。通过对比Python2的旧式实现,重点讲解了利用string.maketrans和str.translate构建翻译表来批量移除标点符号的技巧,并提供完整代码示例及参考链接,帮助开发者快速实现字符串清洗。
网络上关于Python去除字符串标点符号的教程多基于Python2版本,在Python3中往往无法直接适用。经过调整适配后,具体的实现代码如下所示:
代码
lower_case_documents = ['Hello, how are you!','Win money, win from home.','Call me now.','Hello, Call hello you tomorrow?']
sans_punctuation_documents = []
import string
for i in lower_case_documents:
# TODO
trantab = str.maketrans({key: None for key in string.punctuation})
j = i.translate(trantab)
sans_punctuation_documents.append(j)
print(sans_punctuation_documents)
['hello how are you', 'win money win from home', 'call me now', 'hello call hello you tomorrow']
参考
https://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python
以上就是Python3去除字符串中标点符号方法的详细介绍,希望能为各位读者提供实用的参考,同时也欢迎大家继续支持本站获取更多技术内容。
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













