And the pickle module base64

pickle

  • python pickle module is a standard module, provides a sequence of operation for the python data bytes can be converted to the data type, the sequence of which is higher than the velocity json module.
  • the pickle.dumps () to serialize the data bytes type python
  • The pickle.loads () the type of data bytes deserialized data type python

 

base64

  • Base64 is based on 64 printable characters to represent binary data representation. Since 6 = 2 ^ 64, so every six bits as a unit, corresponding to a printable character. 3 bytes of 24 bits, corresponding to four Base64 units, i.e. 3 bytes by four printable characters to represent. Base64 printable characters includes the letters AZ, az, 0-9, so that a total of 62 characters, in addition to two symbols can be printed in various different systems.
  • Base64 commonly used in the normal process in the case of text data, said transmission, storing some binary data, including some complex data MIME email and XML.
  • python standard library provided base64 module for converting
  • base64.b64encode () the data bytes base64 encoding type, returns the encoded bytes type
  • base64.b64deocde () the type or base64 encoded bytes str type decoding, the return type of the decoded bytes

 

Demo:

import base64
import pickle
cart_dict = {1: [2, True], 2: [1, True]}

# dict -> bytes -> str
cart_bytes = pickle.dumps(cart_dict)        # 将python字典转换为16进制bytes类型
print(type(cart_bytes), "|", cart_bytes)    # <class 'bytes'>

cart_b64_bytes = base64.b64encode(cart_bytes)       # 转换为base64_bytes
print(type(cart_b64_bytes), "|", cart_b64_bytes)    # <class 'bytes'>

cart_b64_str = cart_b64_bytes.decode()          # 解码成base64_str
print(type(cart_b64_str), "|", cart_b64_str)    # <class 'str'>

print("=" * 100)

cart_b64_str = "gAN9cQAoSwFdcQEoSwKIZUsCXXECKEsBiGV1Lg=="
# str -> bytes ->dict 
cart_bytes = base64.b64decode (cart_b64_str) # directly convert the string to a hexadecimal bytes base64_str
Print (type (cart_bytes), "|", cart_bytes) # <class 'bytes'> 

cart_dict = The pickle.loads (cart_bytes) # converted to hexadecimal bytes python dictionary 
print (type (cart_dict), " |", cart_dict) # <class 'dict'> 

# It is noted that: in the process of the dictionary obtained python, base64.decode () can be decoded base64_bytes, may be direct decoding base64_str

  

end~

 

 

Guess you like

Origin www.cnblogs.com/kaichenkai/p/10948379.html