Python3 json, logging, SYS modules

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.