python 字符串和文本的一些操作

  1. 针对任意多的分隔符拆分字符串
    python中分隔字符串有 split() 函数,但是如果字符串中包含不同分隔符的话,就需要用到 re.split() 函数。
>>> str = 'asdf fjdk; afed, fjek,asdf,    foo'
>>> import re
>>> re.split('[;,\s]\s*', str)
['asdf', 'fjdk', 'afed', 'fjek', 'asdf', 'foo']
  1. 在字符串的开头或结尾做文本匹配
    python 中 函数 str.startswith() 函数和 str.endswith() 函数可以直接对字符串进行开头结尾匹配。并且可以匹配多个值。
>>> str = 'a.txt'
>>> str.endswith('.txt')
True
>>> str.endswith('.py')
False
>>> str.startswith('a.')
True
>>> str.startswith('b.')
False

要匹配多个值的话,只需要给函数提供多个值组成的元组就可以了。

>>> str_list = ['a.txt', 'a.c', 'a.py', 'a.html', 'a.png', 'a.jpg', 'b.txt', 'b.c', 'b.py', 'b.html', 'b.png', 'b.jpg']
>>> [i for i in str_list if i.endswith(('.txt', '.c'))]
['a.txt', 'a.c', 'b.txt', 'b.c']
>>> [i for i in str_list if i.startswith(('a.t', 'b.t'))]
['a.txt', 'b.txt']
  1. 利用shell通配符做字符串匹配
    linux中shell的通配符是很好用的,那如何在python中使用通配符对字符串进行匹配呢?fnmatch模块提供了两个函数 fnmatch() 和 fnmatchcase()
>>> str = 'aaa.txt'
>>> from fnmatch import fnmatch, fnmatchcase
>>> fnmatch(str, '*.txt')
True
>>> fnmatch(str, 'a*')
True

fnmatchcase()函数与 fnmatch() 函数的区别在于fnmatchcase()函数可以完全根据我们提供的大小写进行匹配。而不用因为系统环境的不同导致匹配出错。
4. 文本模式的匹配和查找
对于简单的文本匹配和查找,可以使用 str.find() str.startswith()等。不过对于复杂的文本匹配则需要用到 re 模块。因为re模块可以使用我们自己写的正则表达式进行匹配。

使用re模块进行匹配的步骤一般为 用re.compile()对正则进行编译,然后使用比如 match(), findall(), finditer() 等方法进行匹配和查找。

>>> str = '19/08/2019'
>>> import re
>>> datepat = re.compile('\d+/\d+/\d+')
>>> print(datepat.match(str))
<re.Match object; span=(0, 10), match='19/08/2019'>
  1. 查找和替换文本
    对于简单的文本替换,使用str.replace()函数即可实现。
>>> str = 'aaaaa,bbbbb,aaaaa'
>>> str.replace('bbbbb', 'aaaaa')
'aaaaa,aaaaa,aaaaa'

对于复杂的文本替换,则需要用到 re 模块中的 sub() 函数

>>> str = '19/08/2019'
>>> import re
>>> re.sub('[0-9]', '_', str)
'__/__/____'
  1. 不区分大小写对文本查找和替换
    进行不区分大小写的文本操作时,需要使用 re 模块,并且各种操作都需要加上 re.IGNORECASE 标记。
>>> str = 'HELLO, hello, Hello'
>>> import re
>>> re.sub('hello', 'world', str, flags=re.IGNORECASE)
'world, world, world'
  1. 定义实现最短匹配的正则表达式
    在对文本进行匹配的时候,有时会出现相匹配的短字符串在相匹配的长字符里。而我们有时需要最短匹配,有时需要最长匹配。比如:
>>> str = '"hello"'
>>> str_1 = '"hello", "world"'
>>> import re
>>> str_pat = re.compile('\"(.*)\"')
>>> str_pat.findall(str)
['hello']
>>> str_pat.findall(str_1)
['hello", "world']

在对 str_1 进行匹配的时候我们需要最短匹配,但是 正则表达式 "(.*)" 中的 * 表示的是贪心策略,所以会匹配最长的结果,我们只需要将表达式变为 “(.*?)” 即可将最长匹配变为最短匹配。

>>> str_pat = re.compile('\"(.*?)\"')
>>> str_pat.findall(str)
['hello']
>>> str_pat.findall(str_1)
['hello', 'world']
  1. 从字符串中去掉不需要的字符
    有时候从文本中读取的字符串开头结尾或者中间会包含空格,换行等字符,需要去掉。开头和结尾的可以使用 strip()函数进行处理,只处理开头使用 lstrip(), 只处理结尾使用 rstrip()。字符串中间的使用 replace()
>>> str = ' hello,   world   \n'
>>> str.strip()
'hello,   world'
>>> str.lstrip()
'hello,   world   \n'
>>> str.rstrip()
' hello,   world'
>>> str.replace(' ', '')
'hello,world\n'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值