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 字典(dict)遍历的四种方法性能测试报告
Jun 25 Python
python中enumerate的用法实例解析
Aug 18 Python
Python爬取网页中的图片(搜狗图片)详解
Mar 23 Python
python的构建工具setup.py的方法使用示例
Oct 23 Python
Python实现的拟合二元一次函数功能示例【基于scipy模块】
May 15 Python
Django REST Framework序列化外键获取外键的值方法
Jul 26 Python
如何获取Python简单for循环索引
Nov 21 Python
TensorFlow命名空间和TensorBoard图节点实例
Jan 23 Python
Keras 数据增强ImageDataGenerator多输入多输出实例
Jul 03 Python
python文件目录操作之os模块
May 08 Python
Python 数据结构之十大经典排序算法一文通关
Oct 16 Python
Python之matplotlib绘制饼图
Apr 13 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
一拳超人中怪人协会钦定! S级别最强四人!
2020/03/02 日漫
php修改时间格式的代码
2011/05/29 PHP
php cURL和Rolling cURL并发方式比较
2013/10/30 PHP
php数组查找函数in_array()、array_search()、array_key_exists()使用实例
2014/04/29 PHP
php使用curl获取https请求的方法
2015/02/11 PHP
PHP实现的网站目录扫描索引工具
2016/09/08 PHP
php将文件夹打包成zip文件的简单实现方法
2016/10/04 PHP
PHP 传输会话curl函数的实例详解
2017/09/12 PHP
PHP7.0连接DB操作实例分析【基于mysqli】
2019/09/26 PHP
常见的5个PHP编码小陋习以及优化实例讲解
2021/02/27 PHP
javascript中var的重要性分析
2015/02/11 Javascript
javascript实现checkBox的全选,反选与赋值
2015/03/12 Javascript
深入分析jQuery的ready函数是如何工作的(工作原理)
2015/12/17 Javascript
Bootstrap实现弹性搜索框
2016/07/11 Javascript
js从数组中删除指定值(不是指定位置)的元素实现代码
2016/09/13 Javascript
Ajax异步文件上传与NodeJS express服务端处理
2017/04/01 NodeJs
JS将unicode码转中文方法
2017/05/08 Javascript
jQuery实现获取选中复选框的值实例详解
2018/06/28 jQuery
如何测量vue应用运行时的性能
2019/06/21 Javascript
原生js+css调节音量滑块
2020/01/15 Javascript
pygame游戏之旅 创建游戏窗口界面
2018/11/20 Python
解析python的局部变量和全局变量
2019/08/15 Python
在Python3 numpy中mean和average的区别详解
2019/08/24 Python
Django 博客实现简单的全文搜索的示例代码
2020/02/17 Python
python pandas移动窗口函数rolling的用法
2020/02/29 Python
Python字符串格式化常用手段及注意事项
2020/06/17 Python
深入浅析python 中的self和cls的区别
2020/06/20 Python
python爬取”顶点小说网“《纯阳剑尊》的示例代码
2020/10/16 Python
节约用水倡议书
2014/04/16 职场文书
十八大演讲稿
2014/05/22 职场文书
高中军训的心得体会
2014/09/01 职场文书
学生不讲诚信检讨书
2014/09/29 职场文书
成事在人观后感
2015/06/16 职场文书
导游词之河北邯郸
2019/09/12 职场文书
多人盗宝《绿林侠盗》第三赛季4.5上线 跨平台实装
2022/04/03 其他游戏
铁头也玩根德 YachtBoy YB-230......
2022/04/05 无线电