• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python中快速进行多个字符替换的方法小结

python中快速进行多个字符替换的方法小结

作者:daisy 字体:[增加 减小] 来源:互联网

daisy 通过本文主要向大家介绍了python函数返回多个值,python 返回多个值,python读取多个文件,python if 多个条件,python打开多个文件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

先给出结论:

  1. 要替换的字符数量不多时,可以直接链式replace()方法进行替换,效率非常高;
  2. 如果要替换的字符数量较多,则推荐在 for 循环中调用 replace() 进行替换。

可行的方法:

1. 链式replace()

string.replace().replace()
</div>

     1.x 在for循环中调用replace() 「在要替换的字符较多时」

2. 使用string.maketrans

3. 先 re.compile 然后 re.sub

……

def a(text):
 chars = "&#"
 for c in chars:
 text = text.replace(c, "\\" + c)
def b(text):
 for ch in ['&','#']:
 if ch in text:
  text = text.replace(ch,"\\"+ch)
import re
def c(text):
 rx = re.compile('([&#])')
 text = rx.sub(r'\\\1', text)
RX = re.compile('([&#])')
def d(text):
 text = RX.sub(r'\\\1', text)
def mk_esc(esc_chars):
 return lambda s: ''.join(['\\' + c if c in esc_chars else c for c in s])
esc = mk_esc('&#')
def e(text):
 esc(text)
def f(text):
 text = text.replace('&', '\&').replace('#', '\#')
def g(text):
 replacements = {"&": "\&", "#": "\#"}
 text = "".join([replacements.get(c, c) for c in text])
def h(text):
 text = text.replace('&', r'\&')
 text = text.replace('#', r'\#')
def i(text):
 text = text.replace('&', r'\&').replace('#', r'\#')
</div>

参考链接:

http://stackoverflow.com/questions/3411771/multiple-character-replace-with-python

http://stackoverflow.com/questions/6116978/python-replace-multiple-strings

http://stackoverflow.com/questions/8687018/python-string-replace-two-things-at-once

http://stackoverflow.com/questions/28775049/most-efficient-way-to-replace-multiple-characters-in-a-string

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用python能有所帮在,如果有疑问大家可以留言交流。

</div>

您可能想查找下面的文章:

  • Python实现将一个大文件按段落分隔为多个小文件的简单操作方法
  • python中快速进行多个字符替换的方法小结
  • Python批量创建迅雷任务及创建多个文件
  • python实现同时给多个变量赋值的方法
  • python实现同时给多个变量赋值的方法
  • Python获取文件ssdeep值的方法
  • Python获取文件ssdeep值的方法

相关文章

  • 关于tf.nn.dynamic_rnn返回值详解
  • python 多线程应用介绍
  • Python实现程序的单一实例用法分析
  • 使用C#配合ArcGIS Engine进行地理信息系统开发
  • Python中的time模块与datetime模块用法总结
  • python访问类中docstring注释的实现方法
  • Python中字符串的处理技巧分享
  • Python生成随机数组的方法小结
  • 用Python解析XML的几种常见方法的介绍
  • 在tensorflow中设置保存checkpoint的最大数量实例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python实现下载文件的三种方法
    • python基于xml parse实现解析cdatasection数据
    • Python入门及进阶笔记 Python 内置函数小结
    • Python深入学习之装饰器
    • Python程序设计入门(2)变量类型简介
    • 深入解析Python中函数的参数与作用域
    • python语言使用技巧分享
    • Python实现的生成自我描述脚本分享(很有意思的程序)
    • python使用opencv进行人脸识别
    • Python环境变量设置方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有