python3去掉string中的标点符号方法

yipeiwu_com6年前Python基础

网上看到的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中的标点符号方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python嵌套函数使用外部函数变量的方法(Python2和Python3)

python嵌套函数使用外部函数变量的方法,Python2和Python3均可使用 python3 def b(): b = 1 def bchange(): nonlo...

Python批量合并有合并单元格的Excel文件详解

Python批量合并有合并单元格的Excel文件详解

合并单元格 合并单元格相信大家都会,比如下面这段简单的代码就可以实现: app='Word' word=win32.gencache.EnsureDispatch('%s.Appl...

python处理multipart/form-data的请求方法

方法1: import requests url = "http://www.xxxx.net/login" #参数拼凑,附件上传格式如picurl参数,其他表单参数值拼成tupl...

Python使用dis模块把Python反编译为字节码的用法详解

dis — Disassembler for Python bytecode,即把python代码反汇编为字节码指令. 使用超级简单: python -m dis xxx.py...

如何利用Python模拟GitHub登录详解

如何利用Python模拟GitHub登录详解

前言 最近学习了Fiddler抓包工具的简单使用,通过抓包,我们可以抓取到HTTP请求,并对其进行分析。现在我准备尝试着结合Python来模拟GitHub登录。 Fiddler抓包分析...