赞
踩
从今天开始,和大家一起学习python中的字符串和文本,本片博客适合有一定python基础的朋友阅读,目的在于灵活运用python中的各种方法,提高代码的健壮性和简洁性。
目录
很多程序都会涉及到某些字符串和文本处理,不管是输入还是产出。接下来的几篇博客重点分享文本的操作处理,比如提取字符串,搜索,替换,解析等等
问题:给定一个含有不规则字符的字符串,要求将这个字符串分割
思路:正常情况下splite方法就可以解决百分之50的字符串分割。但是他只适用于单个规则的分割,如果字符串里面出现了不同规则的分隔符,就可以用我们的re模块并结合我们的正则表达式来解决。
代码如下:
- import re
- str1= 'asd ahsdj;iabi/ihfoi?akshd'
- new_list = re.split(r'[;/?\s]',str1)
- print(new_list) # ['asd', 'ahsdj', 'iabi', 'ihfoi', 'akshd']
注意:re.splite接受两个参数,一个是正则规则,一个是字符串
思考:面试的时候也经常会问道。请说出re.findall、re.search、re.match的用法和区别
问题:想通过匹配字符串的开头或者结尾来确定想要的字符串
思路:这个问题很简单,只需要用我们的startswich和endswich就能解决
代码实现:
- list1= ['1.py','2.c','3.robot','4.txt','5.py','6.jpg']
- for i in list1:
- if i.endswith(".py"):
- print(i) # 1.py 5.py
思考:既然我们可以通过endswich找到我们想要匹配的字符串,那么是不是切片也可以解决此问题呢?
问题:请使用Unix Shell中常用的通配符(*.py,*.csv)去匹配文本字符串,改怎么解决
思路:fnmatch提供了两个方法,fnmatch、fnmatchcase。这两个方法即可解决此类问题
代码实现:
- from fnmatch import fnmatch
- names = ['20220402.txt','DH01.py','DH02.csv','DH5461.py']
- new_names = [name for name in names if fnmatch(name,'DH*.py')]
- print(new_names) # ['DH01.py', 'DH5461.py']
注意:fnmatch接受两个参数,第一个是字符串,第二个是匹配字符串的shell规则。fnmatchcase原理同fnmatch
问题:匹配和搜索特定的字符
思路:这里就是我刚才说到的面试题,find,findall,match,search的使用场景及区别。
代码实现:
- texts = 'today is 2022/04/02,I am very happy because i know a new question in csdn 2022/05/31'
- print(texts.find('csdn')) # 56
- datapat = re.findall(r'\d+/\d+/\d+',texts)
- print(datapat) # ['2022/04/02']
- datapat1 = re.match(r'\d+/\d+/\d+',texts)
- print(datapat1) # None
- datapat2 = re.search(r'\d+/\d+/\d+',texts)
- print(datapat2) # <re.Match object; span=(9, 19), match='2022/04/02'>
总结:通过以上结果。我自己做个自我总结,find返回的是匹配到的字符串出现位置的下标索引。
match的查找方式是从第一个元素开始查找,所以上述正则对他不起作用,返回none,search的查
找方式是找到一个就返回结果。findall的查找方式是找到所有符合当前正则规则的字符串。
问题:给定一个字符串搜索并替换其中的内容
思路:python中的replace就能解决这个问题
代码实现:
- text = 'hi ljj,nice to meet you,i think that today you must commit you homework,' \
- 'beacause it is more than 2022/04/02' \
- 'if you not ok,you will be punished'
- new_text = text.replace('ljj','张三')
- print(new_text)
- """
- hi 张三,nice to meet you,i think that today you must commit you homework,
- beacause it is more than 2022/04/02
- if you not ok,you will be punished
- """
延伸:replace确实可以解决大部分的字符串替换问题,那么如果需要修改有规律的字符串该怎么办呢,re模块中的sub即可解决这个问题
代码实现:
- text = 'hi ljj,nice to meet you,i think that today you must commit you homework,' \
- 'beacause it is more than 2022/04/02' \
- 'if you not ok,you will be punished'
- new_text = text.replace('ljj','张三')
- print(new_text)
- """
- hi 张三,nice to meet you,i think that today you must commit you homework,
- beacause it is more than 2022/04/02
- if you not ok,you will be punished
- """
-
- new_data = re.sub(r'(\d+)/(\d+)/\d+',r'2022/05/02',text)
- print(new_data)
- """
- hi ljj,nice to meet you,i think that today you must commit you homework,
- beacause it is more than 2022/05/02
- if you not ok,you will be punished
- """

觉得还比较有用的小伙伴点赞收藏吧,持续更新中。如果文章中说的有问题的地方,还请大佬指正,欢迎大家和我交流编程相关的知识,也可以给小编提建议,每一位朋友的评论或者私信都会认真解答和讨论的。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。