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之模拟鼠标键盘动作具体实现
Dec 30 Python
pytorch 调整某一维度数据顺序的方法
Dec 08 Python
pyspark操作MongoDB的方法步骤
Jan 04 Python
Python数据可视化之画图
Jan 15 Python
python图像和办公文档处理总结
May 28 Python
django框架自定义模板标签(template tag)操作示例
Jun 24 Python
解决Django中修改js css文件但浏览器无法及时与之改变的问题
Aug 31 Python
python使用协程实现并发操作的方法详解
Dec 27 Python
keras之权重初始化方式
May 21 Python
Python实现弹球小游戏
Aug 01 Python
解决pycharm导入numpy包的和使用时报错:RuntimeError: The current Numpy installation (‘D:\\python3.6\\lib\\site-packa的问题
Dec 08 Python
Python中的socket网络模块介绍
Jul 23 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 图片上添加透明度渐变的效果
2009/06/29 PHP
php的webservice的wsdl的XML无法显示问题的解决方法
2014/03/11 PHP
PHP中定义数组常量(array常量)的方法
2014/11/17 PHP
PHP生成json和xml类型接口数据格式
2015/05/17 PHP
PHP redis实现超迷你全文检索
2017/03/04 PHP
PHP随机获取未被微信屏蔽的域名(微信域名检测)
2017/03/19 PHP
用JavaScript脚本实现Web页面信息交互
2006/10/11 Javascript
新浪微博字数统计 textarea字数统计实现代码
2011/08/28 Javascript
JS解决ie6下png透明的方法实例
2013/08/02 Javascript
JavaScript函数获取事件源的小例子
2014/05/14 Javascript
AngularJS基础教程之简单介绍
2015/09/27 Javascript
Bootstrap插件全集
2016/07/18 Javascript
Nodejs调用WebService的示例代码
2017/09/29 NodeJs
动态加载、移除js/css文件的示例代码
2018/03/20 Javascript
解决vue scoped html样式无效的问题
2020/10/24 Javascript
Python Sleep休眠函数使用简单实例
2015/02/02 Python
Python通过递归遍历出集合中所有元素的方法
2015/02/25 Python
在Python中操作时间之mktime()方法的使用教程
2015/05/22 Python
使用C++扩展Python的功能详解
2018/01/12 Python
Python封装原理与实现方法详解
2018/08/28 Python
pyqt5 tablewidget 利用线程动态刷新数据的方法
2019/06/17 Python
python实现ip代理池功能示例
2019/07/05 Python
使用Python和Scribus创建一个RGB立方体的方法
2019/07/17 Python
基于Python3.6中的OpenCV实现图片色彩空间的转换
2020/02/03 Python
Django多数据库配置及逆向生成model教程
2020/03/28 Python
一个非常简单好用的Python图形界面库(PysimpleGUI)
2020/12/28 Python
HTML5 Canvas概述
2009/08/26 HTML / CSS
JINS眼镜官方网站:日本最大的眼镜邮购
2016/10/14 全球购物
荷兰多品牌网上鞋店:Stoute Schoenen
2017/08/24 全球购物
开放系统互连参考模型
2016/06/29 面试题
工程班组长岗位职责
2013/12/30 职场文书
试用期员工考核制度
2014/01/22 职场文书
2015年办公室个人工作总结
2015/04/20 职场文书
文明礼仪主题班会
2015/08/13 职场文书
MySQL触发器的使用
2021/05/24 MySQL
MySQL串行化隔离级别(间隙锁实现)
2022/06/16 MySQL