Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Going through the string.translate function which says:

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.

  • What does table mean here? Can it be a dict containing the mapping?
  • What does "must be a 256-character string" mean?
  • Can the table be made manually or through a custom function instead of 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?

like image 750
Bleeding Fingers Avatar asked Jan 10 '14 07:01

Bleeding Fingers


People also ask

What is string translate?

The string translate() method returns a string where each character is mapped to its corresponding character in the translation table. translate() method takes the translation table to replace/translate characters in the given string as per the mapping table.

What is translation table in Python?

In simple terms, a translation table is a mapping of one character to another. While working with strings, we may need to replace a character with another character in a string. In such cases, we can use a translation table to determine which character has to be replaced with which character.

What does translate () do in Python?

The translate() method returns a string where some specified characters are replaced with the character described in a dictionary, or in a mapping table. Use the maketrans() method to create a mapping table. If a character is not specified in the dictionary/table, the character will not be replaced.

Which function translates certain characters in a string?

TRANSLATE is a string manipulation function that manipulates all string data types (BIT, BLOB, and CHARACTER), and replaces specified characters in a string.


2 Answers

It depends on Python version you are using.

In Python 2.x. The table is 256-characters string. It can be created using 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.

>>> "abcabc".translate({ord('a'): 'd', ord('c'): 'x'})
'dbxdbx'
like image 104
falsetru Avatar answered Oct 19 '22 02:10

falsetru


table must 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.

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

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

>>> 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 unicode type is the str type in Python 3, that's also how you'd use str.translate() in Python 3 (where bytes.translate() matches the above behaviour).

like image 25
Martijn Pieters Avatar answered Oct 19 '22 03:10

Martijn Pieters