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单元测试框架unittest使用方法讲解
Apr 13 Python
Python Matplotlib库入门指南
May 18 Python
Python 中 list 的各项操作技巧
Apr 13 Python
Python通过属性手段实现只允许调用一次的示例讲解
Apr 21 Python
实例详解Python装饰器与闭包
Jul 29 Python
Python 使用matplotlib模块模拟掷骰子
Aug 08 Python
Python使用selenium + headless chrome获取网页内容的方法示例
Oct 16 Python
解析PyCharm Python运行权限问题
Jan 08 Python
python由已知数组快速生成新数组的方法
Apr 08 Python
python matplotlib实现将图例放在图外
Apr 17 Python
python Scrapy框架原理解析
Jan 04 Python
python_tkinter事件类型详情
Mar 20 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进行MySQL删除记录操作代码
2008/06/07 PHP
php检索或者复制远程文件的方法
2015/03/13 PHP
php基于ob_start(ob_gzhandler)实现网页压缩功能的方法
2017/02/18 PHP
php的常量和变量实例详解
2017/06/27 PHP
分享别人写的一个小型js框架
2007/08/13 Javascript
javascript复制粘贴与clipboardData的使用
2014/10/16 Javascript
详解jQuery中的元素的属性和相关操作
2015/08/14 Javascript
javascript的列表切换【实现代码】
2016/05/03 Javascript
BootStrap 智能表单实战系列(五) 表单依赖插件处理
2016/06/13 Javascript
Vue.js 表单校验插件
2016/08/14 Javascript
JS实现的幻灯片切换显示效果
2016/09/07 Javascript
NodeJS遍历文件生产文件列表功能示例
2017/01/22 NodeJs
使用vue2实现购物车和地址选配功能
2018/03/29 Javascript
vue中js判断长时间不操作界面自动退出登录(推荐)
2020/01/22 Javascript
Vue2.4+新增属性.sync、$attrs、$listeners的具体使用
2020/03/08 Javascript
[02:27]2018DOTA2亚洲邀请赛赛前采访-OpTic
2018/04/03 DOTA
详解Python中的__init__和__new__
2014/03/12 Python
python数据结构树和二叉树简介
2014/04/29 Python
python网络编程学习笔记(八):XML生成与解析(DOM、ElementTree)
2014/06/09 Python
基于python内置函数与匿名函数详解
2018/01/09 Python
Python实现的寻找前5个默尼森数算法示例
2018/03/25 Python
pycharm无法导入本地模块的解决方式
2020/02/12 Python
基于HTML5+CSS3实现简单的时钟效果
2017/09/11 HTML / CSS
BONIA波尼亚新加坡官网:皮革手袋,鞋类和配件
2016/08/25 全球购物
美国最便宜的旅游网站:CheapTickets
2017/07/09 全球购物
Beauty Expert美国/加拿大:购买奢侈美容产品
2018/12/05 全球购物
PatPat香港:婴童服饰和亲子全家装在线购物
2020/09/27 全球购物
C#中的验证控件有几种
2014/03/08 面试题
应届大学生的推荐信
2013/11/20 职场文书
医院总经理职责
2013/12/26 职场文书
办公室文员自荐书
2014/02/03 职场文书
服务理念口号
2014/06/11 职场文书
2014年采购员工作总结
2014/11/18 职场文书
先进个人材料怎么写
2014/12/30 职场文书
毕业生自荐求职信书写的技巧
2019/08/26 职场文书
在项目中使用redis做缓存的一些思路
2021/09/14 Redis