码迷,mamicode.com
首页 > Web开发 > 详细

Python中如何查找和替换文本?

时间:2017-03-25 15:12:58      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:

Python开发中查找和替换非常简单,如果当前对象是一个字符串 str 时,你可以使用该类型提供的 find() 或者 index() 方法查找指定的字符,如果能找到则会返回字符第一次出现的索引,如果不存在则返回-1

>>> s = ’Cat and Dog’>>> s.find(’Dog’)8>>> s.index(’Dog’)8>>> s.find(’Duck’)

-1

如果要替换目标字符串,用 replace() 方法就好了。

>>> s = ’Cat and Dog’>>> s.replace(’Cat’, ’Dog’)’Dog and Dog’

通配符查找匹配

当然,如果你觉得上面的功能还不能满足你,你想使用通配符来查找字符串?没问题! fnmatch 这个库就能满足你的要求,看例子!

>>> s = ’Cat and Dog’>>> import fnmatch>>> fnmatch.fnmatch(s,’Cat*’)

True>>> fnmatch.fnmatch(s,’C*and*D?’)

False>>> fnmatch.fnmatch(s,’C*and*D*’)

True

正则表达式查找替换

如果你需要查找比较复杂的字符规则,正则表达式是你不二的选择。下面是正则查找的简单示例。

>>> import re>>> s = ’We will fly to Thailand on 2016/10/31’>>> pattern = r’\\d+’>>> re.findall(pattern, s)

[’2016’, ’10’, ’31’]>>> re.search(pattern, s)

<_sre.sre_match object="" at="" 0x03a8fd40="">>>> re.search(pattern, s).group()’2016’

接下来你可能需要用正则表达式去替换某些字符,那么你需要了解 re.sub() 方法,看例子。

>>> s = "I like {color} car.">>> re.sub(r’\\{color\\}’,’blue’,s)’I like blue car.’

>>> s = ’We will fly to Thailand on 10/31/2016’>>> re.sub(’(\\d+)/(\\d+)/(\\d+)’, r’\\3-\\1-\\2’, s)’We will fly to Thailand on 2016-10-31’

其实 re.sub() 远比你相像的强大的多。在上面的例子里你可以替换类似于 {color} 这样的模板字符,也可以把正则匹配到的所有分组调换顺序,例如第二个例子一共匹配了3个分组,然后把第3个分组放到最前面 r’\\3-\\1-\\2’ ,看明白了吗?

接下来看另外一个例子。

s = "Tom is talking to Jerry."

name1 = "Tom"

name2 = "Jerry"

pattern = r’(.*)({0})(.*)({1})(.*)’.format(name1, name2)print re.sub(pattern, r’\\1\\4\\3\\2\\5’, s)# Jerry is talking to Tom.

其实你还可以自定义替换函数,也就是 re.sub() 的第二个参数。

defchange_date(m):

from calendar import month_abbr

mon_name = month_abbr[int(m.group(1))]

return ’{} {} {}’.format(m.group(2), mon_name, m.group(3))

s = ’We will fly to Thailand on 10/31/2016’

pattern = r’(\\d+)/(\\d+)/(\\d+)’print re.sub(pattern, change_date, s)# We will fly to Thailand on 31 Oct 2016

最后给大家一个终极版的例子,里面用到了函数的闭包,着酸爽,你懂的!

defmatch_case(word):

defreplace(m):

text = m.group()

if text.isupper():

return word.upper()

elif text.islower():

return word.lower()

elif text[0].isupper():

return word.capitalize()

else:

return word

return replace

s = "LOVE PYTHON, love python, Love Python"

print re.sub(’python’, match_case(’money’), s, flags=re.IGNORECASE)# LOVE MONEY, love money, Love Money

写在最后

其实正则表达式还有很多玩法,如果你想让正则和通配符混合着用,一点问题都没有,因为 fnmatch 还有一个 translate() 的方法,可以让你把通配符无痛转换成正则表达式,你爱怎么玩就怎么玩。

>>> fnmatch.translate(’C*and*D*’)’C.*and.*D.*’

 

来源:测试喵


Python中如何查找和替换文本?

标签:

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
jiangjie190
加入时间:2016-02-19
  关注此人  发短消息
文章分类
jiangjie190”关注的人------(0
jiangjie190”的粉丝们------(0
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!