`
runfeel
  • 浏览: 907238 次
文章分类
社区版块
存档分类
最新评论

string 模块 maketrans函数 和 translate函数的用法

 
阅读更多

1.
string.maketrans(from,to)

Return a translation table suitable for passing totranslate(), that will map each character infrominto the character at the same position into;fromandtomust have the same length.


2.
string.translate(s,table[,deletechars])

Delete all characters from s that are in deletechars (if present), and then translate the characters using table, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. If table isNone, then only the character deletion step is performed.


例子1:

from string import maketrans   # Required to call maketrans function.

intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)

str = "this is string example....wow!!!";
print str.translate(trantab);


输出结果:
>>>
th3s 3s str3ng 2x1mpl2....w4w!!!


例子2:

from string import maketrans   # Required to call maketrans function.

intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)

str = "this is string example....wow!!!";
print str.translate(trantab, 'xm');



输出结果:

th3s 3s str3ng 21pl2....w4w!!!


例子3:
引用自:Python Cookbook 第二版, 1.9节
使用字符串的方法translate,为了使用更方便,对它进行一个封装。

import string
def translator(frm='', to='', delete='', keep=None):
if len(to) == 1:
to = to*len(frm)
trans = string.maketrans(frm, to)

if keep is not None:
#定义一个翻译表allchars,且指定不须翻译。
allchars = string.maketrans('', '')
delete = allchars.translate(allchars, keep.translate(allchars, delete))

def translate(s):
return s.translate(trans, delete)

return translate


NameAndID = 'FuQiang 61450597'

degits_only = translator(keep=string.digits)
print type(degits_only)

ID = degits_only(NameAndID)
print ID

no_digits = translator(delete=string.digits)
name = no_digits(NameAndID)
print name

digits_to_hash = translator(frm=string.digits, to='*')
hideID = digits_to_hash(NameAndID)
print hideID

delete_keep = translator(delete='Fu', keep = 'qiang6145')
partStr = delete_keep(NameAndID)
print partStr

输出结果 :
>>>
<type 'function'>
61450597
FuQiang
FuQiang ********
iang61455
>>>

例子4:
引用自:Python Cookbook 第二版, 1.10节

过滤字符串中不属于指定集合的字符

import string

#maketrans函数是一个创建翻译表的工具函数。
allchars = string.maketrans('', '')

def makefilter(keep):
delchars = allchars.translate(allchars, keep)
def thefilter(s):
return s.translate(allchars, delchars)
return thefilter

if __name__ == '__main__':
filterStr = makefilter('abcdefg')
print filterStr('FuQiang')

输出:
>>>
ag
>>>








分享到:
评论

相关推荐

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

    其中maketrans和translate两个方法被应用的很多,本文就针对这两个方法的用法做一总结整理。 首先让我们先回顾下这两个方法: ① s.translate(table,str) 对字符串s移除str包含的字符,剩下的字符串按照table里的...

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

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

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

    maketrans()方法返回的字符串intab每个字符映射到字符的字符串outtab相同位置的...此方法返回时使用转换表translate()函数。 例子 下面的例子显示maketrans()方法的使用。在此,在一个字符串中每一个元音替换它的元音的

    Python translator使用实例

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

    Python3 菜鸟查询手册

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

    Python示例-从基础到高手PDF

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

    Python字符串替换实例分析

    s.translate(string.maketrans(''.join(a),''.join(b))) print s 输出结果为:abcd 字符串替换,改善版 s = hello, i'm mouren, hehe~~,hehe~~mourenmouren a = [mouren, hehe] b = [mr, hoho] import re di

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

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

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

    先给出结论: 要替换的字符数量不多时,可以直接链式replace()方法进行替换,效率非常...2. 使用string.maketrans 3. 先 re.compile 然后 re.sub …… def a(text): chars = "&#" for c in chars: text = text.rep

    hellow.ko制作

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

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

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

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

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

    易语言透明桌面软件研究

    易语言透明桌面软件研究源码,透明桌面软件研究,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