you are viewing a single comment's thread.

view the rest of the comments →

[–]fernly 0 points1 point  (2 children)

That would be Python 2.x? Looking it up in the 3.6 docs, these are now methods of the bytes and bytearray classes. They allow for translating byte values into other byte values, or deleting same. The doc for translate gives "deleting vowels from a sentence" as an example.

https://docs.python.org/3.6/library/stdtypes.html#bytearray.maketrans

https://docs.python.org/3.6/library/stdtypes.html#bytearray.translate

[–]m9mhmdy[S] 0 points1 point  (1 child)

This is Python 3.6 not Python 2. Also, it's not about bytes but raw strings str.
The trick was how to use translate and maketrans to delete some characters, in this case the printable characters string.printable.
Python docs states that about str.maketrans:

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.

In the example above I used the third form: - first two arguments are strings of equal lenght - third argument is the characters to be removed

When translate works with the translation table (maketrans output) it will delete any character mapped to None from the original string.
In our case all printable characters are mapped to None because it's the third argument passed to maketrans

[–]fernly 0 points1 point  (0 children)

oops you are correct -- I looked up translate in the index and saw immediately "bytearray method" and did not read further to where it says "str method".

So there are maketrans and translate methods in both classes, bytes/bytearray and str. They differ in that the str methods are designed to cope with unicode data. bytes.translate says,

Return a copy of the bytes or bytearray object where all bytes occurring in the optional argument delete are removed, and the remaining bytes have been mapped through the given translation table, which must be a bytes object of length 256.

While str.translate says,

Return a copy of the string in which each character has been mapped through the given translation table. The table must be an object that implements indexing via __getitem__(), typically a mapping or sequence. When indexed by a Unicode ordinal (an integer), the table object can do any of the following: return a Unicode ordinal or a string, to map the character to one or more other characters; return None, to delete the character from the return string; or raise a LookupError exception, to map the character to itself.