python 如何去除字符串头尾的多余符号


Posted in Python onNovember 19, 2019

在读文件时常常得到一些\n和引号之类的符号,可以使用字符串的成员函数strip()来去除。

1.去除首尾不需要的字符

a= '"This is test string"'    # strip()会默认去除'\n','\r','\t',' ',制表回车换行和空格等字符
a.strip('"')
>>> 'This is test string'

b = ' This is another string '  #首尾两个空格
b.strip(' ')
>>>'This is another string'
b.strip()
>>>'This is another string'   # 默认去除

c = '*This is an-another string/'  # 首尾两个字符
c.strip('*/')  #这里strip将解析每一个字符,检查首尾是否存在,存在就去除返回
>>>'This is an-another string'

d = '//This is the last string**'
d.strip('*/')
>>> d = 'This is the last string'  # 持续去除首尾的指定字符符号

e = 'einstance'
e.strip('e')             # 去除首尾特定字符
>>> 'instanc'

2.去除末尾特定字符

专治末尾多余字符rstrip()

a = ' example '
a.rstrip()   #同样默认去除末尾的空格\n,\t,\r
>>>' example'
b = 'this is mya'
b.rstrip('a') #去除末尾特定字符
>>>'this is my'

3.去除开头特定字符

专治开头多余字符lstrip()

a = ' example '
a.lstrip()   #默认去除开头的空格\n,\t,\r
>>>'example '
b = 'athis is mya'
b.lstrip('a') #去除末尾特定字符
>>>'this is mya'

4.去除字符串中的特定字符

一种常见的方法是转换为list,再使用remove方法,随后再转换为string,这里再额外说明两种方法。使用replace()和re.sub()

# 使用字符串replace()方法,将目标字符替换为空
a = 'this is the test'
a.replace('t','')
>>>'his is he es'

#第二种方法使用正则表达式方法
import re
re.sub('s','', a)
>>>'thi i the tet'

5.巧用eval()函数

eval函数的作用是将传入的字符串作为表达式来进行计算,可以有效去除(双)引号,空格等字符。

a = ' "This is a good example" ' 
eval(a)
>>>`This is a good example`
b = '    "This is a good example" ' 
eval(b)
>>>'This is a good example'

重要提示:字符串外面的引号和字符串内的引号不能同时使用单引号或双引号,外面用了单引号里面只能用双引号,否则会引起异常。

总结

以上所述是小编给大家介绍的python 如何去除字符串头尾的多余符号,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Python 相关文章推荐
Python中的map()函数和reduce()函数的用法
Apr 27 Python
python中sleep函数用法实例分析
Apr 29 Python
Python基于动态规划算法计算单词距离
Jul 25 Python
python操作oracle的完整教程分享
Jan 30 Python
对Python中type打开文件的方式介绍
Apr 28 Python
pytorch对可变长度序列的处理方法详解
Dec 08 Python
django之状态保持-使用redis存储session的例子
Jul 28 Python
通过selenium抓取某东的TT购买记录并分析趋势过程解析
Aug 15 Python
Python Matplotlib绘图基础知识代码解析
Aug 31 Python
Django执行源生mysql语句实现过程解析
Nov 12 Python
关于pycharm 切换 python3.9 报错 ‘HTMLParser‘ object has no attribute ‘unescape‘ 的问题
Nov 24 Python
Python OpenCV实现图形检测示例详解
Apr 08 Python
wxPython实现画图板
Aug 27 #Python
如何修复使用 Python ORM 工具 SQLAlchemy 时的常见陷阱
Nov 19 #Python
Python高级特性之闭包与装饰器实例详解
Nov 19 #Python
Python高级编程之继承问题详解(super与mro)
Nov 19 #Python
Python3 Tkinkter + SQLite实现登录和注册界面
Nov 19 #Python
Python csv文件的读写操作实例详解
Nov 19 #Python
Python高级property属性用法实例分析
Nov 19 #Python
You might like
PHP+Mysql+jQuery实现发布微博程序 jQuery篇
2011/10/08 PHP
基于PHP CURL用法的深入分析
2013/06/09 PHP
详细解读php的命名空间(一)
2018/02/21 PHP
JavaScript中出现乱码的处理心得
2009/12/24 Javascript
javascript实现tabs选项卡切换效果(自写原生js)
2013/03/19 Javascript
$.getJSON在IE下失效的原因分析及解决方法
2013/06/16 Javascript
jQuery事件用法实例汇总
2014/08/29 Javascript
在jQuery中使用$而避免跟其它库产生冲突的方法
2015/08/13 Javascript
解读Bootstrap v4 sass设计
2016/05/29 Javascript
全面了解js中的script标签
2016/07/04 Javascript
Laravel中常见的错误与解决方法小结
2016/08/30 Javascript
ES6模块化的import和export用法方法总结
2017/08/08 Javascript
JavaScript中this关键字用法实例分析
2018/08/24 Javascript
Vue页面跳转动画效果的实现方法
2018/09/23 Javascript
VUE UPLOAD 通过ACTION返回上传结果操作
2020/09/07 Javascript
解决Antd Table表头加Icon和气泡提示的坑
2020/11/17 Javascript
Vue router传递参数并解决刷新页面参数丢失问题
2020/12/02 Vue.js
[02:37]TI8勇士令状不朽珍藏II视频展示
2018/06/23 DOTA
python实现根据图标提取分类应用程序实例
2014/09/28 Python
python使用in操作符时元组和数组的区别分析
2015/05/19 Python
python实现批量下载新浪博客的方法
2015/06/15 Python
Python制作Windows系统服务
2017/03/25 Python
Python2中文处理纪要的实现方法
2018/03/10 Python
对python抓取需要登录网站数据的方法详解
2018/05/21 Python
Python第三方库face_recognition在windows上的安装过程
2019/05/03 Python
浅谈Python 递归算法指归
2019/08/22 Python
在TensorFlow中屏蔽warning的方式
2020/02/04 Python
django admin管理工具自定义时间区间筛选器DateRangeFilter介绍
2020/05/19 Python
PyCharm+PyQt5+QtDesigner配置详解
2020/08/12 Python
python 模拟登录B站的示例代码
2020/12/15 Python
Python爬虫UA伪装爬取的实例讲解
2021/02/19 Python
UGG英国官方网站:UGG UK
2018/02/08 全球购物
自荐信封面
2013/12/04 职场文书
考试作弊检讨书大全
2014/02/18 职场文书
认真学习保证书
2015/02/26 职场文书
飞越疯人院观后感
2015/06/09 职场文书