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实现数值积分方式

Python实现数值积分方式

原理: 利用复化梯形公式,复化Simpson公式,计算积分。 步骤: import math """测试函数""" def f(x,i): if i == 1: re...

python能做什么 python的含义

python能做什么?是什么意思? Python是一种跨平台的计算机程序设计语言。是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能...

Python常用模块os.path之文件及路径操作方法

以下是 os.path 模块的几种常用方法: 方法 说明 os.path.abspath(path...

Flask-Mail用法实例分析

Flask-Mail用法实例分析

本文实例讲述了Flask-Mail用法。分享给大家供大家参考,具体如下: 很多类型的应用程序都需要在特定事件发生时提醒用户,而常用的通信方法是电子邮件。 虽然 Python 标准库中的...

python 使用正则表达式按照多个空格分割字符的实例

python 使用正则表达式按照多个空格分割字符的实例

程序代码如下 import os import re os.system("nmap -sP 192.168.3.0/24") //扫描IP mac = os.popen("cat...