Python string.translate 函数中的“table”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21038891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:53:11  来源:igfitidea点击:

What does "table" in the string.translate function mean?

pythonstringpython-2.7

提问by Bleeding Fingers

Going through the string.translatefunction which says:

通过string.translate功能说:

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 is None, then only the character deletion step is performed.

从 s 中删除 deletechars 中的所有字符(如果存在),然后使用 table 转换字符,它必须是一个 256 字符的字符串,给出每个字符值的转换,按其序数索引。如果 table 为 None,则仅执行字符删除步骤。

  • What does tablemean here? Can it be a dictcontaining the mapping?
  • What does "must be a 256-character string"mean?
  • Can the tablebe made manually or through a custom function instead of string.maketrans?
  • 在这里是什么意思?它可以是一个dict包含映射吗?
  • 什么是“必须是256个字符的字符串”是什么意思?
  • 表格可以手动制作或通过自定义功能制作string.maketrans吗?

I tried using the function (attempts below) just to see how it worked but wasn't successfully able to use it.

我尝试使用该功能(下面的尝试)只是为了查看它是如何工作的,但未能成功使用它。

>>> "abcabc".translate("abcabc",{ord("a"): "d", ord("c"): "x"})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: translation table must be 256 characters long
>>> "abcabc".translate({ord("a"): ord("d"), ord("c"): ord("x")}, "b")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected a character buffer object

>>> "abc".translate({"a": "d", "c": "x"}, ["b"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected a character buffer object

What am I missing here?

我在这里缺少什么?

回答by falsetru

It depends on Python version you are using.

这取决于您使用的 Python 版本。

In Python 2.x. The table is 256-characters string. It can be created using string.maketrans:

在 Python 2.x 中。该表是 256 个字符的字符串。它可以使用string.maketrans以下方法创建:

>>> import string
>>> tbl = string.maketrans('ac', 'dx')
>>> "abcabc".translate(tbl)
'dbxdbx'

In Python 3.x, the table is mapping of unicode ordinals to unicode characters.

在 Python 3.x 中,该表是 unicode 序数到 unicode 字符的映射。

>>> "abcabc".translate({ord('a'): 'd', ord('c'): 'x'})
'dbxdbx'

回答by Martijn Pieters

tablemust be a string of 256 characters; the str.translate()method uses this table to map the byte value (a number between 0 and 255) to a new character; e.g. any character 'a'(a byte with the integer value 97) is replaced with the 98th character in the table.

table必须是 256 个字符的字符串;该str.translate()方法使用此表将字节值(0 到 255 之间的数字)映射到新字符;例如,任何字符'a'(整数值为 97 的字节)都被替换为表中的第 98 个字符。

You really want to refer to the str.translate()documentationfor all this, not the string.translate()function; the latter documentation is not as complete.

你真的想参考所有这些的str.translate()文档,而不是string.translate()函数;后者的文档并不完整。

You can build one using string.maketransfunction; you give it justthe characters you want to replace with the characters that replace these; for your example, that's:

您可以使用string.maketrans函数构建一个;你给它只是你想与替换这些字符替换字符; 对于你的例子,那就是:

>>> import string
>>> table = string.maketrans('ac', 'cx')
>>> len(table)
256
>>> table[97]
'c'
>>> 'abcabc'.translate(table, 'b')
'cxcx'

The second argument is also supposed to be a string.

第二个参数也应该是一个字符串。

You appear to have read the documentation for the unicode.translate()method; behaviour changed and you indeed have to pass in a dictionary for unicode.translate(). Since the Python 2 unicodetype is the strtype in Python 3, that's also how you'd use str.translate()in Python 3 (where bytes.translate()matches the above behaviour).

您似乎已阅读该unicode.translate()方法的文档;行为发生了变化,您确实必须传入unicode.translate(). 由于 Python 2unicode类型是strPython 3 中的类型,这也是您str.translate()在 Python 3 中使用的方式(bytes.translate()与上述行为匹配)。

回答by Sébastien Wieckowski

To translate text, not using a dictionary {ordinal: char}, but a dictionary {char: char} (e.g. {'a': 'X', 'J': 'y', ...}:

要翻译文本,不使用字典 {ordinal: char},而是使用字典 {char: char}(例如 {'a': 'X', 'J': 'y', ...}:

text.translate({ord(k):dictionary[k] for k in dictionary})