python去掉标点、特殊符号_python3去掉string中的标点符号方法

网上看到的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去掉string中的标点符号方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

本文标题: python3去掉string中的标点符号方法

本文地址: http://www.cppcns.com/jiaoben/python/250749.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python,可以使用不同的方法去掉字符串标点符号。一种方法是使用Python自带的punctuation包,该包可以消除所有标点符号和英文标点符号。你可以导入re和string模块,并使用re.sub()函数来实现。下面是一个示例代码: ```python import re import string from zhon.hanzi import punctuation text = "Hello, world! 这,是:我;第!一个程序\?()()<>《》" clean_text = re.sub(r"\[%s\]+" % punctuation, "", text) print(clean_text) ``` 另一种方法是自己定义标点符号集,既可以消除标点符号也可以消除英文标点符号。你可以导入re和string模块,并使用re.sub()函数来实现。下面是一个示例代码: ```python import re import string text = "Hello, world! 这,是:我;第!一个程序\?()()<>《》" punc = '~`!#$%^&*()_+-=|\';":/.,?><~·!@#¥%……&*()——+-=“:’;、。,?》《{}' clean_text = re.sub(r"\[%s\]+" % punc, "", text) print(clean_text) ``` 以上两种方法都可以去掉字符串标点符号。你可以根据自己的需求选择其一种方法来使用。 #### 引用[.reference_title] - *1* *3* [Python去除文本所有标点符号](https://blog.csdn.net/haiziccc/article/details/101027675)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [chatgpt赋能pythonPython如何删除字符串标点符号](https://blog.csdn.net/www_xuhss_com/article/details/131149195)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值