`
txf2004
  • 浏览: 6880284 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Python——maketrans和translate方法,可以用来从string中替换和删除字符

 
阅读更多
maketrans和translate是密切相关的两个方法,先看translate的说明

S.translate(table [,deletechars]) -> string

Return a copy of the string S, where all characters occurring
in the optional argument deletechars are removed, and the
remaining characters have been mapped through the given
translation table, which must be a string of length 256.

简单来说就是对字符串S移除deletechars包含的字符,然后保留下来的字符按照table里面的字符映射关系映射(比如a变成A,后面会解释到)。那个莫名其妙的"which must be a string of length 256"就不用深究了,反正table就是由string.maketrans方法生成的,对于string.maketrans方法,这里有个更清晰的解释,如下:

string.maketrans(intab, outtab) --> This method returns a translation table that maps each character in the intab string into the character at the same position in the outtab string. Then this table is passed to the translate() function. Note that both intab and outtab must have the same length.
下面是一些实例说明:

import string

s = 'abcdefg-1234567'
table = string.maketrans('', '') #没有映射,实际上就是按原始字符保留,看下面用到translate中时的效果
s.translate(table) # 输出abcdefg-1234567
s.translate(table, 'abc123') #输出defg-4567 可以看到删除了字符abc123

#下面再看有字符映射时的效果
table = string.maketrans('abc', 'ABC') #用于translate中时的效果如下
s.translate(table) #输出ABCdefg-1234567 就是将abc映射为大写的ABC,前提是abc如果被保留下来了
s.translate(table, 'ab123') #输出Cdefg-4567 先把s中的ab123去除了,然后在保留下来的字符中应用table中指定的字符映射关系映射:c -> C



解释完毕,现在知道为什么maketrans(intab, outtab)中两个字符串参数长度必须一样了,因为是字符一对一的映射。

神奇的Python中总是有很多奇怪的存在
分享到:
评论

相关推荐

    闭包在python中的应用之translate和maketrans用法详解

    相对来说python对字符串的处理是比较高效的,方法也有很多。...② tabel = string.maketrans(‘s1’, ‘s2’) s1 和 s2 的长度必须一致,maketrans生成一个转换表,若在s中有s1,则替换为s2,这个转换表是字符字符

    Python字符串替换实例分析

    本文实例讲述了Python字符串替换的方法。分享给大家供大家参考。具体如下: 单个字符替换 s = 'abcd' a = [a, b, c] b = [c, d, e] import string s.translate(string.maketrans(''.join(a),''.join(b))) print s ...

    Python示例-从基础到高手PDF

    第 6 章 python 中用 string.maketrans 和 translate 巧妙替换字符串 第 7 章 python linecache 模块读取文件用法详解 第 8 章 python 调用 zabbix 的 api 接口添加主机、查询组、主机、模板 第 9 章 python+...

    Python2.x和3.x下maketrans与translate函数使用上的不同

    maketrans和translate函数是进行字符串字符编码的常用方法。本文着重点在于演示其基本用法和在不同版本下操作的差异。本文提到的2.X版本指2.6以上的版本,3.X版本指3.1以上的版本。  2.X版本把字符串基本分为两种:...

    Python2.x版本中maketrans()方法的使用介绍

    主要介绍了Python2.x版本中maketrans()方法的使用介绍,是Python学习中的基础知识,需要的朋友可以参考下

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

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

    Python translator使用实例

    复制代码 代码如下:allchars = string.maketrans(”, ”)#所有的字符串,即不替换字符串 aTob = string.maketrans(‘a’,’b’)#将字符a转换为字符b 2.translate函数进行字符串的替换和删除,第一个参数是字符串转换...

    python3去掉string中的标点符号方法

    网上看到的python去掉字符串中的标点符号的方法,大多是基于python2的,不适用python3,调整后代码如下: 代码 lower_case_documents = ['Hello, how are you!','Win money, win from home.','Call me now.','Hello...

    Python3 菜鸟查询手册

    08.23 字符串内建函数 maketrans()方法.png 08.24 字符串内建函数 max()方法.png 08.25 字符串内建函数 min()方法.png 08.26 字符串内建函数 replace()方法.png 08.27 字符串内建函数 rfind()方法.png 08.28 ...

    PowerShellTranslate:PowerShell Invoke-Translate 和 New-TranslationTable 通常用于替换密码

    Python 语言具有通常用于替换密码的translate和maketrans函数。在 PowerShell 中使用它下面的例子展示了 Invoke-Translate 的用法。 在这里,字符串中的每个元音都被其元音位置替换。 $InputTable = " aeiou " $...

    详解Python3.1版本带来的核心变化

    主要介绍了详解Python3.1版本带来的核心变化,Python3.1的版本升级在3.0的基础上带来了更多影响以后版本的变化,本文分析了其中一些常用功能的改变,如Maketrans函数等,需要的朋友可以参考下

    hellow.ko制作

    hellow.ko制作。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。...

    易语言透明桌面软件研究

    易语言透明桌面软件研究源码,透明桌面软件研究,MakeTrans,设主窗透明度,Ansi转Unicode,BitBlt,DeleteObject,SetWindowLong,AlphaBlend,UpdateLayeredWindow,CreateDIBSection,GetDIBits,SetDIBits,...

    易语言VISTA模拟窗口

    易语言VISTA模拟窗口源码,VISTA模拟窗口,MakeTrans,PaintForm,DrawCaptionAndIcon,ColorARGB,ASCII转Unicode,初始化变量,LoadResData_path,LoadResData_data,数据处理,ReadResData,取数量,WndProc,MousePos,...

Global site tag (gtag.js) - Google Analytics