First, today's main content:
Module: A py file is a module
Divided into three categories: 1, built-in modules: Login module, TIME module, SYS module, OS module, etc. are built-in modules
2, Expansion module: Third-party modules that need to be downloaded
3, Custom module: Write your own module.
1, serialization module: JSON, Pickle, shelve (Learn)
Serialization: Creating a sequence (a sequence of string types) a data type---> serialized string
Deserialization process: serialized string---> The type of data she corresponds to.
JSON: Applies to different languages, but the supported data types are relatively rare and only: strings, numbers, lists, dictionaries. There are 4 ways: for network transmission: dumps () <--> loads () for access to files: Dump () <--> load ()
1, the data is sent to someone via the network:
Json.dumps (the content that needs to be converted to JSON.) The resulting string type, but this string, not the string, is The serialized string, which can be sent directly on the network, can also be understood by other languages, and the difference between the string and the string is: This string is a double quote, and the string is a single quote
Json.loads (the data that was Json.dumps): the deserialization process.
2, write the file.
Json.dump (obj, fq) obj = object, Fq = File handle. The serialized string is written to the file
Json.load (FQ) FQ = file handle, read, deserialized String,
PS: Writes multiple serialized strings to the file and then deserializes, an error occurs. If you really want to write more than one, you need to use dumps and loads, if it is a dictionary, key must be a string
Pickle: The serialization module is used only between Python and can support all Python data types. There are also 4 methods (two pairs) for network transmission: Dumps () <-->loads () for file operation: dump<---load ()
1, Network transmission
Pickle.dumps () will be converted into bytes type and cannot be decode.
Pickle.loads () converts the bytes into a data type that can be viewed and used.
2, file operation
Pickle.dump ()
Pickle.load ()
PS: These two are better than JSON, he can read a lot of different lists, but
Shelve: Only for Python, more like a gadget. FILE-related
2, abstract algorithm: Hashlib
Oldboy 23th Day. I Love Python. Module initialization, and serialization module