CONVERTING BACK AND FORTH BETWEEN STRING AND BYTES: >>> s = 'hello' >>> s.encode() b'hello' >>> b = s.encode() >>> b.decode() 'hello' >>> --- --- --- $ conda create --name py2 python=2 (base) ~/Desktop$ conda activate py2 By default, 'python' command runs Python 3. (py2) ~/Desktop$ python Python 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> exit() (py2) ~/Desktop$ python2 Python 2.7.18 |Anaconda, Inc.| (default, Apr 23 2020, 22:42:48) [GCC 7.3.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import requests Traceback (most recent call last): File "[stdin]", line 1, in [module] ImportError: No module named requests >>> --- --- --- (py2) ~/Desktop$ pip install requests DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support Collecting requests Using cached https://files.pythonhosted.org/packages/29/c1/24814557f1d22c56d50280771a17307e6bf87b70727d975fd6b2ce6b014a/requests-2.25.1-py2.py3-none-any.whl Collecting chardet<5,>=3.0.2 Using cached https://files.pythonhosted.org/packages/19/c7/fa589626997dd07bd87d9269342ccb74b1720384a4d739a1872bd84fbe68/chardet-4.0.0-py2.py3-none-any.whl Collecting urllib3<1.27,>=1.21.1 Using cached https://files.pythonhosted.org/packages/23/fc/8a49991f7905261f9ca9df5aa9b58363c3c821ce3e7f671895442b7100f2/urllib3-1.26.3-py2.py3-none-any.whl Collecting idna<3,>=2.5 Using cached https://files.pythonhosted.org/packages/a2/38/928ddce2273eaa564f6f50de919327bf3a00f091b5baba8dfa9460f3a8a8/idna-2.10-py2.py3-none-any.whl Requirement already satisfied: certifi>=2017.4.17 in /home/administrator/anaconda3/envs/py2/lib/python2.7/site-packages (from requests) (2020.6.20) Installing collected packages: chardet, urllib3, idna, requests Successfully installed chardet-4.0.0 idna-2.10 requests-2.25.1 urllib3-1.26.3 --- --- --- With Python 2 (py2) ~/Desktop$ python2 Python 2.7.18 |Anaconda, Inc.| (default, Apr 23 2020, 22:42:48) [GCC 7.3.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import requests >>> from base64 import b64encode as b >>> b('hello') 'aGVsbG8=' >>> --- --- --- With Python 3 (py2) ~/Desktop$ python Python 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from base64 import b64encode as b >>> b('hello') Traceback (most recent call last): File "[stdin]", line 1, in [module] File "/usr/lib/python3.8/base64.py", line 58, in b64encode encoded = binascii.b2a_base64(s, newline=False) TypeError: a bytes-like object is required, not 'str' >>> b('hello'.encode()) b'aGVsbG8=' >>> b('hello'.encode()).decode() 'aGVsbG8=' >>> --- --- ---
Wednesday, February 24, 2021
Python 2, python 3, base64, string and bytes
Labels:
Python,
Technology
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment