How to encode text to base64 in python

前端 未结 8 2112
悲&欢浪女
悲&欢浪女 2020-12-24 01:17

I am trying to encode a text string to base64.

i tried doing this :

name = \"your name\"
print(\'encoding %s in base64 yields = %s\\n\'%(name,name.en         


        
相关标签:
8条回答
  • 2020-12-24 01:22

    Whilst you can of course use the base64 module, you can also to use the codecs module (referred to in your error message) for binary encodings (meaning non-standard & non-text encodings).

    For example:

    import codecs
    my_bytes = b"Hello World!"
    codecs.encode(my_bytes, "base64")
    codecs.encode(my_bytes, "hex")
    codecs.encode(my_bytes, "zip")
    codecs.encode(my_bytes, "bz2")
    

    This can come in useful for large data as you can chain them to get compressed and json-serializable values:

    my_large_bytes = my_bytes * 10000
    codecs.decode(
        codecs.encode(
            codecs.encode(
                my_large_bytes,
                "zip"
            ),
            "base64"),
        "utf8"
    )
    

    Refs:

    • https://docs.python.org/3/library/codecs.html#binary-transforms
    • https://docs.python.org/3/library/codecs.html#standard-encodings
    • https://docs.python.org/3/library/codecs.html#text-encodings
    0 讨论(0)
  • 2020-12-24 01:23

    It turns out that this is important enough to get it's own module...

    import base64
    base64.b64encode(b'your name')  # b'eW91ciBuYW1l'
    base64.b64encode('your name'.encode('ascii'))  # b'eW91ciBuYW1l'
    
    0 讨论(0)
  • 2020-12-24 01:23

    To compatibility with both py2 and py3

    import six
    import base64
    
    def b64encode(source):
        if six.PY3:
            source = source.encode('utf-8')
        content = base64.b64encode(source).decode('utf-8')
    
    0 讨论(0)
  • 2020-12-24 01:25

    For py3, base64 encode and decode string:

    import base64
    
    def b64e(s):
        return base64.b64encode(s.encode()).decode()
    
    
    def b64d(s):
        return base64.b64decode(s).decode()
    
    0 讨论(0)
  • 2020-12-24 01:35

    Use the below code:

    import base64
    
    #Taking input through the terminal.
    welcomeInput= raw_input("Enter 1 to convert String to Base64, 2 to convert Base64 to String: ") 
    
    if(int(welcomeInput)==1 or int(welcomeInput)==2):
        #Code to Convert String to Base 64.
        if int(welcomeInput)==1:
            inputString= raw_input("Enter the String to be converted to Base64:") 
            base64Value = base64.b64encode(inputString.encode())
            print "Base64 Value = " + base64Value
        #Code to Convert Base 64 to String.
        elif int(welcomeInput)==2:
            inputString= raw_input("Enter the Base64 value to be converted to String:") 
            stringValue = base64.b64decode(inputString).decode('utf-8')
            print "Base64 Value = " + stringValue
    
    else:
        print "Please enter a valid value."
    
    0 讨论(0)
  • 2020-12-24 01:38

    It looks it's essential to call decode() function to make use of actual string data even after calling base64.b64decode over base64 encoded string. Because never forget it always return bytes literals.

    import base64
    conv_bytes = bytes('your string', 'utf-8')
    print(conv_bytes)                                 # b'your string'
    encoded_str = base64.b64encode(conv_bytes)
    print(encoded_str)                                # b'eW91ciBzdHJpbmc='
    print(base64.b64decode(encoded_str))              # b'your string'
    print(base64.b64decode(encoded_str).decode())     # your string
    
    0 讨论(0)
提交回复
热议问题