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

yipeiwu_com5年前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设计】。

相关文章

TensorFlow实现AutoEncoder自编码器

TensorFlow实现AutoEncoder自编码器

一、概述 AutoEncoder大致是一个将数据的高维特征进行压缩降维编码,再经过相反的解码过程的一种学习方法。学习过程中通过解码得到的最终结果与原数据进行比较,通过修正权重偏置参数降低...

python+tkinter编写电脑桌面放大镜程序实例代码

python+tkinter编写电脑桌面放大镜程序实例代码

本文讲述的是通过python+tkinter编写一个简单桌面放大镜的代码示例,具体如下。 代码思路:首先全屏截图,然后在鼠标当前位置以小窗口进行二次截图,放大后再显示到鼠标左上角。 主要...

Python字节单位转换实例

我就废话不多说了,直接上代码! from enum import Enum class Values(): values={'B':1} @staticmethod...

Python 处理数据的实例详解

Python 处理数据的实例详解 最近用python(3.2的版本)写了根据特定规则,处理数据的一个小程序,用到了一些python常用的基础知识,在此总结一下: 1,python读文件...

Cpy和Python的效率对比

Python 语言的初学者, 特别是"惊奇者"(也就是那种第一眼就被毫无意义的某些特性吸引, 之后持续说服自己的人)认为 Python 不需要 C 语言的 for 语句, 因为他们能用优...