什么是Python string.maketrans(“”,“”)

时间:2016-04-06 23:37:03

标签: python string python-2.7 punctuation

 string.maketrans("","")

给出

 \x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13 
 \x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?
 @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~
 \x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90
\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2
\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4
\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9
\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde
\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed
\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff

这是什么意思?

如何通过以下调用删除字符串中的标点符号有帮助:

 import string
 myStr.translate(string.maketrans("",""), string.punctuation)

3 个答案:

答案 0 :(得分:7)

我会采取一些自由,因为Python 2混淆了字符串和字节。有256个字节,范围从0到255.您可以使用chr()获取其字节表示。所以,0到255之间的所有字节都是这样的

>>> ''.join(map(chr, range(256)))
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\
x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;
<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80
\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93
\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6
\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9
\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc
\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf
\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2
\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'

string.maketrans(from, to)创建一个包含256个字符的字符串,其中from中的字符将替换为to。例如,string.maketrans('ab01', 'AB89')将从上方返回字符串,但a将被A替换,b将被B替换,0将被8替换为1。 {1}}和9 >>> string.maketrans('ab01', 'AB89') '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\ x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./8923456789:; <=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`ABcdefghijklmnopqrstuvwxyz{|}~\x7f\x80 \x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93 \x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6 \xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9 \xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc \xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf \xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2 \xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'

string.maketrans('', '') == ''.join(map(chr, range(256)))

有效地,a

这用作地图,当提供给str.translate()时,它可用于替换多个字符,一次通过字符串。对于上面的示例地图,除了从Ab转变为B等所有myStr.translate(string.maketrans('', ''))之外,所有字符都将保持不变,等等。{{1} },你只是不改变myStr中的任何内容。

最后,translate()还有一个参数deletechars。如果您为该参数传递字符串,translate()将根据您提供的映射转换所有字符,但它将忽略deletechars中的任何字符。因此,将它们放在一起,myStr.translate(string.maketrans('', ''), string.punctuation)不会更改字符串中的任何字符,但在此过程中将忽略string.punctuation中的任何字符。实际上,您已删除输出字符串中的标点符号。

答案 1 :(得分:0)

string.maketrans(intab, outtab)返回一个转换表,该表将intabstring中的每个字符映射到outtab字符串中相同位置的字符。

tran_table = string.maketrans(intab, outtab)
print myStr.translate(tran_table)

然后,上面的代码将使用您创建的表格翻译myStr。在您的情况下,表格会生成所有字符,因为您没有指定任何字符。

答案 2 :(得分:0)

Python 2.7&#39; s string.maketrans()返回一个字节值,就像您的结果一样,可以与string.translate()一起使用。

string.translate(s, table)s中的字符(我们称之为c)翻译为table[ord(c)]。因此\x00被翻译为table[0],依此类推。在您的情况下,它只是返回一个身份表。

应该注意的是,在Python 2.7中不推荐使用string.translate,在Python 3.1及更高版本中,它们被bytes.maketrans()bytes.translate()替换为{{1}的相应方法}} str