Python AttributeError: module 'string' has no attribute 'maketrans'
maketrans
is deprecated in favor of new static methods
The
string.maketrans()
function is deprecated and is replaced by new static methods,bytes.maketrans()
andbytearray.maketrans()
. This change solves the confusion around which types were supported by thestring
module. Now,str
,bytes
, andbytearray
each have their ownmaketrans
andtranslate
methods with intermediate translation tables of the appropriate type.
You can use dir()
to verify that whenever you have this kind of issue:
>>> import string>>>>>> dir(string)['Formatter', 'Template', '_ChainMap', '_TemplateMetaclass', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']>>>
As you can see, there is no maketrans
in the resulted list above.