The road to wudao-the five-day post of the refining stage
Serialization: changes an object from memory to a stored and transmitted process, and vice versa.
The standard format of objects transmitted between different programming languages is json (and xml). However, in python, json can only serialize simple objects. in complicated cases, pickle is generally used (it may not be deserialized)
For json, double quotation marks are recommended for dictionary serialization. Important Methods: dumps () and dump ()-> serialization, loads () and load ()-> deserialization,
Dump converts a python object to a json object to generate a fp file stream, while dumps generates a string, which is the same as load and loads.
Import json, pickle
Dict1 = {"name": "abc", "score": 77}
With open('test.txt ', 'w') as f:
F. write (json. dumps (dict1) # Same as pickle, here is the write
Json. dump (dict1, f) # Same as pickle, write
Json. loads (f. read (), json. load (f) read
Module: essentially a. py file. To implement some functions (variables, functions, classes), the file name is test. py. The module name is test.
Package: essentially a directory or folder, which must have a _ init _. py file (with some modules)
Introduction:
Import x, y, z as m references multiple modules, and as takes the alias
Import test to test. the code of the py file is interpreted once and assigned to test (call the variable: test. variable name. Call the function: test. function Name (), the same as from test import * (paste the code here. We do not recommend this to avoid repeated variable functions)
From test import x (variables or methods under test. aliases can be used as if duplicate names are prevented)
The essence of the import package is to execute the _ init _. py file under it.
Sometimes the import module is not in the same directory or under it:
Imporyt OS, sys
_ File _ relative path of the current program
OS. path. abspath (_ file _) absolute path of the current program
OS. path. dirname (_ file _) remove the last path
Sys. path. append (url) to set environment variables.