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

相关文章

举例讲解Python程序与系统shell交互的方式

概述 考虑这样一个问题,有hello.py脚本,输出”hello, world!”;有TestInput.py脚本,等待用户输入,然后打印用户输入的数据。那么,怎么样把hello.py输...

python模块和包的应用BASE_PATH使用解析

这篇文章主要介绍了python模块和包的应用BASE_PATH使用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 python中的...

python获取一组汉字拼音首字母的方法

本文实例讲述了python获取一组汉字拼音首字母的方法。分享给大家供大家参考。具体实现方法如下: #!/usr/bin/env python # -*- coding: utf-8...

基于Python中单例模式的几种实现方式及优化详解

单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在。当你希望在整个系统中,某个类只能出现一个实例时,单例对象...

python查找第k小元素代码分享

复制代码 代码如下:# -*- coding: utf-8 -*- from random import randintfrom math import ceil, floor def...