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. NPOI 计算单元格高度

    需求 要导出一个Excel,第一行是不定长字符串,合并单元格(A-G)已知,现要计算第一行的高度. 思路 已知,NPOI的宽度单位是1/256个英文字符,在本例中,A列的宽度是2048,即 2048 ...

  2. javascript判断浏览器核心

    20 21 22 23 24 /** * 判断浏览器核心 * @return IE6.0/IE7.0/IE8.0/FireFox/Opera/other * @author ypz */ functi ...

  3. hibernate单向多对一映射

    n21: 1.new 两个实体类,一个代表"多"的一端,一个代表"一"的一端. Customer类: public class Customer { priva ...

  4. eclipse JVM Tomcat 内存堆栈大小设置

    1,  设置Eclipse内存使用情况 修改eclipse根目录下的eclipse.ini文件 -vmargs  //虚拟机设置 -Xms40m //初始内存 -Xmx256m //最大内存 -Xmn ...

  5. linux flush memcache缓存

    telnet localhost 11211 flush_all  memcached Telnet Interface Command Description Example get Reads a ...

  6. Android备份和添加短信

    手机发送成功的,没有成功的,接受的短信都存放在手机自带的数据库中. 现在想要备份一下这个短信,需要访问这个数据库,然后查询所有的短信.就需要内容提供者(短信). 首先要找到内容提供者的uri.

  7. shadow密码文件

    登录Linux会要求输入用户名和密码.通常本地文件中会存储一份用户密码,并与用户输入对比,如果相同就允许用户登录.起初用户密码存储与/etc/passwd中,但由于/etc/passwd必须供所有用户 ...

  8. Windows系统SVN服务器搭建与使用

    下载svn:https://tortoisesvn.net/downloads.zh.html下载svn服务器:https://www.visualsvn.com/server/download/(如 ...

  9. Nodejs通过thrift访问Java服务

    上一篇文章中实现了用Java作为thrift客户端和服务端.接下来我们用nodejs作为客户端访问一下.Nodejs的安装可以查看http://www.cnblogs.com/xucheng/p/39 ...

  10. [CTSC2001]1378 选课

      1378 选课 题目描述 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修了这 ...