maketrans 和 translate的用法(配合使用)

下面是python的英文用法解释

maketrans(x, y=None, z=None, /)
Return a translation table usable for str.translate().

If there is only one argument, it must be a dictionary mapping Unicode
ordinals (integers) or characters to Unicode ordinals, strings or None.
Character keys will be then converted to ordinals.
If there are two arguments, they must be strings of equal length, and
in the resulting dictionary, each character in x will be mapped to the
character at the same position in y. If there is a third argument, it
must be a string, whose characters will be mapped to None in the result

S.translate(table) -> str

Return a copy of the string S, where all characters have been mapped
through the given translation table, which must be a mapping of
Unicode ordinals to Unicode ordinals, strings, or None.
Unmapped characters are left untouched. Characters mapped to None
are deleted.

makestans返回一个给translate用的映射表,translate根据映射表构造新的字符串。

makestran根据参数的个数有三种使用方法:

1)一个参数情况,参数必须是字典

一个字符转换成一个字符

 >>> a='qwerabc2348'
>>> d={'a':'A','q':'Q'} #转换映射表
>>> tans=str.maketrans(d) #转换为translate可以使用的映射表
>>> tans
{: 'A', : 'Q'} #translate可以使用的映射表
>>> a.translate(tans)
'QwerAbc2348' #转换后的结果

一个字符转换为多个字符

 >>> d2={'a':'*A*','q':'*Q*'}
>>> tans2=str.maketrans(d2)
>>> tans2
{: '*A*', : '*Q*'}
>>> a.translate(tans2)
'*Q*wer*A*bc2348

一个字符转换为None,效果为过滤删除字符

 >>> d3={'a':None,'q':None}
>>> tans3=str.maketrans(d3)
>>> tans3
{: None, : None}
>>> a.translate(tans3)
'werbc2348'

2)两个参数的情况,参数(字符串)必须长度相等。

 >>> a='acbsdwf124'
>>> tans4=str.makestrans('abc','ABC')
>>> tans4=str.maketrans('abc','ABC')
>>> tans4
{: , : , : }
>>> a.translate(tans4)
'ACBsdwf124'

3)三个参数的情况,前两个参数效果和2)相同,第三个参数为要过滤删除的字符表(第三个参数都映射为None)

 >>> a
'acbsdwf124'
>>> tans5=str.maketrans('abc','ABC','')
>>> tans5
{: , : , : , : None, : None, : None, : None}
>>> a.translate(tans5)
'ACBsdwf'

4)映射表中的数字为unicode编码数字

 >>> ord('a')

 >>> chr()
'a'

python3 字符串属性(三)的更多相关文章

  1. python3 字符串属性(一)

    python3 字符串属性 >>> a='hello world' >>> dir(a) ['__add__', '__class__', '__contains_ ...

  2. python3 字符串属性(四)

    1. S.partition(sep) -> (head, sep, tail) Search for the separator sep in S, and return the part b ...

  3. python3字符串属性(二)

    1.S.isdecimal() -> bool    Return True if there are only decimal characters in S, False otherwise ...

  4. 彻底了解构建 JSON 字符串的三种方式

    原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7701856.html 前言:JSON 是轻量级的数据交换格式,很常用,尤其是在使用 Ajax ...

  5. python系列四:Python3字符串

    #!/usr/bin/python #Python3 字符串#可以截取字符串的一部分并与其他字段拼接var1 = 'Hello World!'print ("已更新字符串 : ", ...

  6. Python3字符串 详解

    Python3 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可. Python 访问字符串中的值 P ...

  7. NSAttributedString字符串属性类

    //定义一个可变字符串属性对象aStr NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithStri ...

  8. 字符串属性使用strong的原因

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  9. mysql 连接命令 表管理 ,克隆表,临时表,字符串属性,设定语句间的分隔符

    连接和断开连接mysql -h host -u user -p (即,连接的主机.用户名和使用的密码).断开输入QUIT (或\q)随时退出: 表管理克隆表注意:create table ... li ...

随机推荐

  1. Borg Maze - poj 3026(BFS + Kruskal 算法)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9821   Accepted: 3283 Description The B ...

  2. win10 下eclipse tomcat 热部署问题?

    前言: 问题的描述: 用的环境是maven,java,tomcat,win10 tomcat server配置如下 项目发布之后,修改jsp,报错,错误详情如下: 解决办法.勾选server opti ...

  3. 【SQLServer2008】之如何导入mdf,ldf文件、导入.mdf文件报错解决问题以及如何获得Authenticated Users 权限

    一.如何导入mdf,ldf文件: 链接:http://jingyan.baidu.com/article/09ea3ede21258cc0afde3943.html 二.导入.mdf文件报错解决问题 ...

  4. COM线程单元

    节选自C#高级编程 不管是单线程单元还是多线程单元,一个线程只能属于一个单元. 1) 单线程单元(apartment, 寓所,套间) 单线程单元与它拥有的线程是一对一的关系.COM对象在编写时不是线程 ...

  5. android Notification 的使用(锁定通知栏)

    近期一直在研究 android .并一边研究一边做应用.当中遇到了把程序通知常驻在 Notification 栏,而且不能被 clear 掉(就像android QQ一样)的问题.经过研究实现了其功能 ...

  6. php闭包简单实例

    <?php function getClosure($i) { $i = $i.'-'.date('H:i:s'); return function ($param) use ($i) { ec ...

  7. 脑法之中的一个 --- DEBUG与搜索算法

    前年闲的蛋疼的时候,看过天津卫视的一档节目<非你莫属>,就来一堆面试者,上面几个壕.选人.记得有一期是给程序猿做的.当中有一个程序猿(好像是媛)傻不啦叽的说,哎呀,我每次DEBUG找到程序 ...

  8. ajax 和jsonp 不是一码事 细读详解

    由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socket通讯 ...

  9. 重载(overload)、覆盖(override)和隐藏(hide)

    写正题之前,先给出几个关键字的中英文对照,重载(overload),覆盖(override),隐藏(hide).在早期的C++书籍中,可能翻译的人不熟悉专业用语(也不能怪他们,他们不是搞计算机编程的, ...

  10. POJ 1584 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】

    链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...