JSON module
ImportJsondic= {'name':'Egon',' Age': 32}#------------------------------> Serializationf = open ('Json.txt','W') Data=json.dumps (DIC)Print(data)Print(Type (data)) F.write (data) f.close ()#------------------------------> Return serializationf = open ('Json.txt','R') Data=f.read () Dic1=json.loads (data)Print(DIC1)Print(Type (DIC1)) F.close ()
Second, logging module
ImportLogginglogger=Logging.getlogger () FH= Logging. Filehandler ('Test.log')#for writing to log filesch = logging. Streamhandler ()#for input to terminal#Custom FormatsFormatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s') Fh.setformatter (formatter) sh.setformatter (formatter) logger.addhandler (FH)#Logger object can add multiple FH and SH objectsLogger.addhandler (SH) logger.debug ('Logger Debug Message') Logger.info ('Logger Info Message') logger.warning ('Logger warning message') Logger.error ('Logger error message') logger.critical ('Logger Critical Message')
Third, SYS module
Import Sys
sys.exit (n) exit program, Exit normally (0)
sys.argv command line parameter list, The first element is the path of the program itself
sys.path Returns the search path for the module, using the value of the PYTHONPATH environment variable when initializing
sys.exit ()
Import syscount=1 while count<10: Print(count) if count ==8: sys.exit () # Exit program count+=1print("ending")
sys.argv
#cmd terminal input python sys module. PY Egon 666Print("begin ... ..") ret=sys.argv#[sys module. PY ', "Egon", 666]Print(ret)#[' sys module. PY ', '-u ', ' Egon ', '-P ', ' 666 ']username=ret[1]password=ret[2]ifusername=='Egon' andpassword=="666": Print("Login")
Sys.path
Import SYS Print (Sys.path) # ["C:\Users\Administrator\PycharmProjects\python5 period \day13", "python Environment"] : The directory where the files are executed is added to the Sys.pathBase _dir=r"C:\Users\Administrator\PycharmProjects\python5 period \day12" Sys.path.append (base_dir)import lesson1 # Custom Module Lesson1.foo ()
Python3 json, logging, SYS modules