Как сделать кодировку Base32 в python3? - PullRequest
0 голосов
/ 19 января 2019

Кодировка Base32 в python 2.7 работает следующим образом:

$ python
Python 2.7.10 (default, Oct  6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import base64
>>> print(base64.b32encode("abc"))
MFRGG===

Но когда я пытаюсь сделать то же самое в python3, это не получается.Почему?

$ python
Python 3.7.2 (default, Jan 13 2019, 12:50:15)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import base64
>>> print(base64.b32encode("abc"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "my-virtual-env/lib/python3.7/base64.py", line 154, in b32encode
    s = memoryview(s).tobytes()
TypeError: memoryview: a bytes-like object is required, not 'str'

1 Ответ

0 голосов
/ 19 января 2019

Ответ:

print(base64.b32encode(bytearray("abc", 'ascii')).decode('utf-8'))
...