python删除字符串中指定字符的方法


Posted in Python onAugust 13, 2018

最近开始学机器学习,学习分析垃圾邮件,其中有一部分是要求去除一段字符中的标点符号,查了一下,网上的大多很复杂例如这样

import re 
temp = "司法局让我和户 1 5. 8 0. !!?? 客户维护户外" 
temp = temp.decode("utf8") 
string = re.sub("[\s+\.\!\/_,$%^*(+\"\']+|[+——!,。?、~@#¥%……&*()]+".decode("utf8"), "".decode("utf8"),temp) 
print string

或者是这样的

'''引入string模块'''
import string
'''使用标点符号常量'''
string.punctuation
text = "*/@》--【】--12()测试*()"

'''去除字符串中所有的字符,可增加自定义字符'''
def strclear(text,newsign=''):
  import string # 引入string模块
  signtext = string.punctuation + newsign # 引入英文符号常量,可附加自定义字符,默认为空
  signrepl = '@'*len(signtext) # 引入符号列表长度的替换字符
  signtable = str.maketrans(signtext,signrepl) # 生成替换字符表
  return text.translate(signtable).replace('@','') # 最后将替换字符替换为空即可

strclear(text,'》【】')

我一开始用的后面的这个,着实是有点暴力,于是找了查了一下原文档,发现python3中完全有更好的方法去实现这样的功能(似乎是新更新的?不太清楚,我的是python最新版本3.6.6)

和上面的方法一样是利用的是str的translate()和maketrans()

translate()自然不用说这里的重点是maketrans(),先放上官方的文档

static str.maketrans(x[, y[, z]])
This static method returns a translation table usable for str.translate().

If there is only one argument, 
it must be a dictionary mapping Unicode ordinals (integers) or characters (strings of length 1) to Unicode ordinals, 
strings (of arbitrary lengths) or None. Character keys will then be converted to ordinals.

If there are two arguments, 
they must be strings of equal length, 
and in the resulting dictionary, 
each character in x will be mapped to the character at the same position in y. 
If there is a third argument, it must be a string, whose characters will be mapped to None in the result.

可以看出maketrans是可以放三个参数的(以前一直以为只有两个....)

前两个参数是需要一一对应进行替换,需要字符串长度相同

第三个参数是直接替换为None

这里就直接上代码了

import string

i = 'Hello, how are you!'

i.translate(str.maketrans('', '', string.punctuation))
>>>'Hello how are you'

 i = 'hello world i am li'
 i.translate(str.maketrans('','','l'))

>>>'heo word i am i'

这里的string.punctuation 是python内置的标点符号的合集

既然看到了就总结下

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python的正则表达式re模块的常用方法
Mar 09 Python
python判断给定的字符串是否是有效日期的方法
May 13 Python
浅析python中的分片与截断序列
Aug 09 Python
Python使用struct处理二进制的实例详解
Sep 11 Python
Python实现读取txt文件并画三维图简单代码示例
Dec 09 Python
Python爬虫番外篇之Cookie和Session详解
Dec 27 Python
python2.6.6如何升级到python2.7.14
Apr 08 Python
pytest中文文档之编写断言
Sep 12 Python
python使用openCV遍历文件夹里所有视频文件并保存成图片
Jan 14 Python
python十进制转二进制的详解
Feb 07 Python
如何使用python的ctypes调用医保中心的dll动态库下载医保中心的账单
May 24 Python
matplotlib运行时配置(Runtime Configuration,rc)参数rcParams解析
Jan 05 Python
Django contenttypes 框架详解(小结)
Aug 13 #Python
Python中的Numpy矩阵操作
Aug 12 #Python
浅谈python之新式类
Aug 12 #Python
详解Django中类视图使用装饰器的方式
Aug 12 #Python
python中pip的安装与使用教程
Aug 10 #Python
python3判断url链接是否为404的方法
Aug 10 #Python
Python实现数据可视化看如何监控你的爬虫状态【推荐】
Aug 10 #Python
You might like
无线电的诞生过程
2021/03/01 无线电
微信自定义菜单的处理开发示例
2015/04/16 PHP
php使用curl打开https网站的方法
2015/06/17 PHP
js 省地市级联选择
2010/02/07 Javascript
js获取当前时间显示在页面上并每秒刷新
2014/12/24 Javascript
JavaScript实现俄罗斯方块游戏过程分析及源码分享
2015/03/23 Javascript
JavaScript多并发问题如何处理
2015/10/28 Javascript
nodejs初步体验篇
2015/11/23 NodeJs
微信小程序 rpx 尺寸单位详细介绍
2016/10/13 Javascript
JS实现搜索关键词的智能提示功能
2017/07/07 Javascript
Vue脚手架的简单使用实例
2018/07/10 Javascript
JavaScript事件发布/订阅模式原理与用法分析
2018/08/21 Javascript
在vue中给列表中的奇数行添加class的实现方法
2018/09/05 Javascript
Element UI框架中巧用树选择器的实现
2018/12/12 Javascript
使用Vue.observable()进行状态管理的实例代码详解
2019/05/26 Javascript
通过vue手动封装on、emit、off的代码详解
2019/05/29 Javascript
跟老齐学Python之大话题小函数(2)
2014/10/10 Python
python cx_Oracle的基础使用方法(连接和增删改查)
2017/11/19 Python
python监控进程脚本
2018/04/12 Python
Python常见MongoDB数据库操作实例总结
2018/07/24 Python
Python实现查找最小的k个数示例【两种解法】
2019/01/08 Python
Python这样操作能存储100多万行的xlsx文件
2019/04/16 Python
简单了解python关系(比较)运算符
2019/07/08 Python
Python while true实现爬虫定时任务
2020/06/08 Python
Python虚拟环境库virtualenvwrapper安装及使用
2020/06/17 Python
荟萃全球保健品:维他购
2018/05/09 全球购物
家得宝官网:The Home Depot(全球最大的家居装饰专业零售商)
2018/12/17 全球购物
新闻记者个人求职的自我评价
2013/11/28 职场文书
大学三年的自我评价
2013/12/25 职场文书
房地产广告策划方案
2014/05/15 职场文书
汽车检测与维修专业求职信
2014/07/04 职场文书
幸福家庭事迹材料
2014/12/20 职场文书
党支部工作总结2015
2015/04/01 职场文书
闪闪的红星观后感
2015/06/08 职场文书
《蚂蚁和蝈蝈》教学反思
2016/02/22 职场文书
看古人们是如何赞美老师的?
2019/07/08 职场文书