one, sys moduleSYS module is a module that interacts with the Python interpreter and is commonly used in the following waysSYS.ARGV: Used to receive parameters from outside the program while the program is running, if the parameter is not passed, the program's file name is displayed.
Import sysname=sys.argv[1]password=sys.argv[2]if name== ' jly ' and password== ' 123 ': print (' program continues ') Else: Sys.exit ()--------------------------------------------------------------------------------------Console execution Results g:\ learning \ Python Project \first>python3 g:\ Learning \python Project \first\august\day21\t21.py jly 123 program Continue execution
sys.exit (n): Used to exit the Python program, the default normal exit return number 0, error returned 1, you can specify the number returned when exiting to determine the reason for exitsys.version: Get the Python interpreter versionsys.size: Gets the maximum number of digits for a Python-supported integerSys.path: The search path used to get the Python moduleSys.platform: Get operating system platform name second, serialization moduleserialization is the process of translating dictionaries and lists into string form2.1. JSON moduleJSON is the most commonly used module for serialization because its serialization results are common to all languages, and JSON provides four ways of loads, dumps, load, dumpdumps: Used to serialize a dictionary, list to a string, and returnloads: Used to convert serialized strings to dictionaries and lists
dic={' name ': ' Alex ', ' Age ': 23}dic_str=json.dumps (DIC) dic_dic=json.loads (dic_str) print (Dic_str,type (DIC_STR)) Print (Dic_dic,type (dic_dic))---------------------------------------------------------------------------------- ----{"name": "Alex", "Age": $ <class ' str ' >{' name ': ' Alex ', ' age ': $ <class ' dict ' >
dump:dump function is similar to dumps, but it does not directly return the result, but instead writes the serialized result to the specified fileLoad: Reads the serialized content from the specified file and translates it into a dictionary or list
F=open (' json ', ' W ') dic={' name ': ' Alex ', ' Age ': 23}json.dump (dic,f) f.close () F=open (' json ') dic=json.load (f) f.close ()
2.2. Pickle Moduleas with JSON, just the result of serialization is the bytes type of data only Python can use 2.3. Shelve Moduleshelve is also a module for serialization, it has only one open method, access to the data via key
F=shelve.open (' shelve_file ') f[' K2 ']={' int ': Ten, ' float ': 9.5, ' string ': ' Sample data '}f.close () f=shelve.open (' Shelve _file ') ext=f[' K2 ']f.close () print (EXT)
because shelve only allows one thread to write to it at the same time, we need to set it to read-only mode when opening the shelve file to avoid affecting other people writing it. shelve default does not record changes to the original key value, such as f[' k2][' new ']= ' This is not a here before ', the default is not to write to the file, if you need to write the changes to the file will need to set writeback to True , such as F=shelve.open (' Shelve_file ', writeback=true)
The SYS module and the serialization module of Python's common modules