Python中的文本替换

2023-01-31 02:01:51 python 文本 替换

文本替换是字符串的基本操作,python的str提供了replace方法:

src = '那个人看起来好像一条狗,哈哈'
print(src.replace(',哈哈', '.'))

上面代码最后的输出结果是:

那个人看起来好像一条狗.

对于习惯了Java中的replace,Python的replace用起来有些不适应,因为后者不支持直接使用正则表达式。要实现通过正则表达式的替换,可以配合Python的正则表达式模块使用。比如:

"""
替换掉字符串value内竖线之后的的内容
"""
import re

src = '[{"name":"date","value":"2017数据"},{"name":"年收入","value":"3000|{"url":"Http://www.abc.com/chart/income","x":"0","y":"456.172820"}"},{"name":"税款","value":"-"},{"name":"人数","value":"3419"},{"name":"部门","value":"27|{"url":"http://www.abc.com/department/list","x":"0","y":"155.852820"}"},{"name":"部门营收","value":"-"}]'

#通过则表达式查找,如果有多个匹配,只能获取到最后一个
matches = re.search('\|{.*?"}', src)
while matches is not None:
    src = src.replace(matches.group(0), '')
    matches = re.search('\|{.*?"}', src)

print(src)

最后的结果:

[{"name":"date","value":"2017数据"},{"name":"年收入","value":"3000"},{"name":"税款","value":"-"},{"name":"人数","value":"3419"},{"name":"部门","value":"27"},{"name":"部门营收","value":"-"}]

相关文章