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接收Gmail新邮件并发送到gtalk的方法
Mar 10 Python
python清除指定目录内所有文件中script的方法
Jun 30 Python
python中验证码连通域分割的方法详解
Jun 04 Python
python高阶爬虫实战分析
Jul 29 Python
使用TensorFlow实现SVM
Sep 06 Python
使用Fabric自动化部署Django项目的实现
Sep 27 Python
Python上下文管理器全实例详解
Nov 12 Python
Python3标准库之threading进程中管理并发操作方法
Mar 30 Python
Python之变量类型和if判断方式
May 05 Python
Python Django中间件使用原理及流程分析
Jun 13 Python
简单了解Django项目应用创建过程
Jul 06 Python
使用pandas生成/读取csv文件的方法实例
Jul 09 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出现Cannot modify header information问题的解决方法大全
2008/04/09 PHP
浅析php中常量,变量的作用域和生存周期
2013/08/10 PHP
百度站点地图(百度sitemap)生成方法分享
2014/01/09 PHP
PHP curl使用实例
2015/07/02 PHP
PHP抓取淘宝商品的用户晒单评论+图片+搜索商品列表实例
2016/04/14 PHP
PHP实现路由映射到指定控制器
2016/08/13 PHP
PHP命名空间namespace用法实例分析
2016/09/27 PHP
JavaScript 继承详解 第一篇
2009/08/30 Javascript
当滚动条滚动到页面底部自动加载增加内容的js代码
2014/05/13 Javascript
javascript抽象工厂模式详细说明
2014/12/16 Javascript
JS动态添加Table的TR,TD实现方法
2015/01/28 Javascript
seajs加载jquery时提示$ is not a function该怎么解决
2015/10/23 Javascript
JavaScript操作URL的相关内容集锦
2015/10/29 Javascript
vue+axios新手实践实现登陆的示例代码
2018/06/06 Javascript
详解vue+axios给开发环境和生产环境配置不同的接口地址
2019/08/16 Javascript
vue引用外部JS的两种种方法
2020/01/28 Javascript
JavaScript实现简易聊天对话框(加滚动条)
2020/02/10 Javascript
实例讲解python函数式编程
2014/06/09 Python
Python常见数据结构详解
2014/07/24 Python
Python中获取网页状态码的两个方法
2014/11/03 Python
Python3中bytes类型转换为str类型
2018/09/27 Python
Python格式化字符串f-string概览(小结)
2019/06/18 Python
python中几种自动微分库解析
2019/08/29 Python
在Ubuntu 20.04中安装Pycharm 2020.1的图文教程
2020/04/30 Python
Python正则表达式如何匹配中文
2020/05/27 Python
Python matplotlib 绘制双Y轴曲线图的示例代码
2020/06/12 Python
详解CSS3的perspective属性设置3D变换距离的方法
2016/05/23 HTML / CSS
用HTML5制作烟火效果的教程
2015/05/12 HTML / CSS
美赞臣新加坡官方旗舰店:Enfagrow新加坡
2019/05/15 全球购物
Orlebar Brown官网:设计师泳裤和泳装
2020/12/08 全球购物
C面试题
2015/10/08 面试题
感情真挚的毕业生求职信
2014/07/19 职场文书
民事诉讼代理委托书
2014/10/08 职场文书
仓管员岗位职责范本
2015/04/01 职场文书
美容院管理规章制度
2015/08/05 职场文书
vue自定义右键菜单之全局实现
2022/04/09 Vue.js