Commit 454636f0 authored by Georg Brandl's avatar Georg Brandl

Document bytes.translate().

BTW, having str.maketrans() as a static method and
string.maketrans() as a function that creates translation tables for bytes objects is not very consistent :)
parent 09923f3b
...@@ -1116,13 +1116,12 @@ functions based on regular expressions. ...@@ -1116,13 +1116,12 @@ functions based on regular expressions.
.. method:: str.translate(map) .. method:: str.translate(map)
Return a copy of the *s* where all characters have been mapped through the Return a copy of the *s* where all characters have been mapped through the
*map* which must be a dictionary of Unicode ordinals(integers) to Unicode *map* which must be a dictionary of Unicode ordinals (integers) to Unicode
ordinals, strings or ``None``. Unmapped characters are left untouched. ordinals, strings or ``None``. Unmapped characters are left untouched.
Characters mapped to ``None`` are deleted. Characters mapped to ``None`` are deleted.
You can use :meth:`str.maketrans` to create a translation table. For string You can use :meth:`str.maketrans` to create a translation map from
objects, set the *table* argument to ``None`` for translations that only character-to-character mappings in different formats.
delete characters:
.. note:: .. note::
...@@ -1525,23 +1524,23 @@ The bytes and bytearray types have an additional class method: ...@@ -1525,23 +1524,23 @@ The bytes and bytearray types have an additional class method:
>>> bytes.fromhex('f0 f1f2 ') >>> bytes.fromhex('f0 f1f2 ')
b'\xf0\xf1\xf2' b'\xf0\xf1\xf2'
.. XXX verify/document translate() semantics! The translate method differs in semantics from the version available on strings:
.. method:: bytes.translate(table[, delete]) .. method:: bytes.translate(table[, delete])
Return a copy of the bytes object where all bytes occurring in the optional Return a copy of the bytes or bytearray object where all bytes occurring in
argument *delete* are removed, and the remaining bytes have been mapped the optional argument *delete* are removed, and the remaining bytes have been
through the given translation table, which must be a bytes object of length mapped through the given translation table, which must be a bytes object of
256. length 256.
You can use the :func:`maketrans` helper function in the :mod:`string` module to You can use the :func:`string.maketrans` helper function to create a
create a translation table. translation table.
.. XXX a None table doesn't seem to be supported Set the *table* argument to ``None`` for translations that only delete
Set the *table* argument to ``None`` for translations that only delete characters:: characters::
>>> 'read this short text'.translate(None, 'aeiou') >>> b'read this short text'.translate(None, b'aeiou')
'rd ths shrt txt' b'rd ths shrt txt'
.. _types-set: .. _types-set:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment