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中编写ORM框架的入门指引
Apr 29 Python
python绘制条形图方法代码详解
Dec 19 Python
python画出三角形外接圆和内切圆的方法
Jan 25 Python
对Python2与Python3中__bool__方法的差异详解
Nov 01 Python
python使用adbapi实现MySQL数据库的异步存储
Mar 19 Python
python实现人工智能Ai抠图功能
Sep 05 Python
Python搭建代理IP池实现存储IP的方法
Oct 27 Python
如何修复使用 Python ORM 工具 SQLAlchemy 时的常见陷阱
Nov 19 Python
Python中itertools的用法详解
Feb 07 Python
python如何调用php文件中的函数详解
Dec 29 Python
python 实现百度网盘非会员上传超过500个文件的方法
Jan 07 Python
Python如何让字典保持有序排列
Apr 29 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
PHP中for循环语句的几种变型
2006/11/26 PHP
一段防盗连的PHP代码
2006/12/06 PHP
PHP统一页面编码避免乱码问题
2015/04/09 PHP
php获取网站根目录物理路径的几种方法(推荐)
2017/03/04 PHP
jQuery的一些注意
2006/12/06 Javascript
jQuery.Autocomplete实现自动完成功能(详解)
2010/07/13 Javascript
jQuery中index()的用法分析
2014/09/05 Javascript
推荐25个超炫的jQuery网格插件
2014/11/28 Javascript
谈谈JavaScript自定义回调函数
2015/10/18 Javascript
JavaScript函数的调用以及参数传递
2015/10/21 Javascript
JavaScript获取当前cpu使用率的方法
2015/12/15 Javascript
Javascript 普通函数和构造函数的区别
2016/11/05 Javascript
JS简单实现移动端日历功能示例
2016/12/28 Javascript
JS获取当前地理位置的方法
2017/10/25 Javascript
layer弹出层 iframe层去掉滚动条的实例代码
2018/08/17 Javascript
微信小程序当前时间时段选择器插件使用方法详解
2018/12/28 Javascript
在Create React App中启用Sass和Less的方法示例
2019/01/16 Javascript
详解关于微信setData回调函数中的坑
2019/02/18 Javascript
vue项目中使用scss的方法步骤
2019/05/16 Javascript
Vue 实现一个命令式弹窗组件功能
2019/09/25 Javascript
JavaScript碰撞检测原理及其实现代码
2020/03/12 Javascript
vue3.0实现插件封装
2020/12/14 Vue.js
vue3弹出层V3Popup实例详解
2021/01/04 Vue.js
便捷提取python导入包的属性方法
2018/10/15 Python
python实现转圈打印矩阵
2019/03/02 Python
python代码编写计算器小程序
2020/03/30 Python
Python如何计算语句执行时间
2019/11/22 Python
PyCharm取消波浪线、下划线和中划线的实现
2020/03/03 Python
Python函数的迭代器与生成器的示例代码
2020/06/18 Python
BISSELL官网:北美吸尘器第一品牌
2019/03/14 全球购物
大学生安全责任书
2014/07/25 职场文书
2014年民警工作总结
2014/11/25 职场文书
师德承诺书2015
2015/04/28 职场文书
家长必看:义务教育,不得以面试 评测等名义选拔学生
2019/07/09 职场文书
为Java项目添加Redis缓存的方法
2021/05/18 Redis
MySQL一些常用高级SQL语句
2021/07/03 MySQL