SYS module
SYS.ARGV command line argument list, the first element is the program itself path Sys.exit (n) exits the program, exit normally (0)
Sys.path Returns the search path for the module, using the value of the PYTHONPATH environment variable when initializing
========================================================SYS.ARGV de effect
The returned command is a list that assigns a user name and password directly to the list
-------------------------------------Sys.path Add the path to the module, import using
============================logging Log Module
import logging
to write the level of the log file, increasing the permissions in turn Logging.debug ( Span style= "COLOR: #800000" > ' debug message ) logging.info ( ' info Message ' ) logging.warning ( ' warning message ' ) logging.error ( ' error message" ) logging.critical ( ' Critical Message
The log is divided into 5 levels, from low to High, respectively: DEBUG INFO WARNING ERROR CRITICAL.
DEBUG: Detailed information, usually appearing only on diagnostic issues
INFO: Make sure everything works as expected
WARNING: A sign that some unexpected things have happened, or indicate some problems in the near future (for example. Low disk space "). The software will work as expected.
ERROR: A more serious problem, the software does not perform some functions
CRITICAL: A serious error that indicates that the program itself may not continue to run
These 5 levels, also correspond to 5 kinds of methods to hit the log: Debug, info, warning, error, critical. The default is warning, which is tracked when warning or above
Three levels of default printing
= = File output to screen with Basicconfig module
Write the log in the file--------use the Basicconfig module
Log file fixed format, Basicconfig can only be written on the screen or in a file, is defective
ImportLogging Logging.basicconfig (level=Logging. DEBUG, format=‘% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s‘, datefmt=‘%a,%d%b%Y%h:%m:%s‘, filename=‘/tmp/test.log‘, filemode= "w" ) Logging.debug ( ' debug message ") logging.info (" info message ") logging.warning ( Span style= "COLOR: #800000" > ' warning message ") logging.error ( ' error Message ' ) logging.critical ( "critical message
Because the level, paper print error
----Output with Logging.getlogger ()
Define a function Write log file
Def get_logger ():
Logger_obj=logging.getlogger ()
PrintType (logger_obj))
Fh=logging. Filehandler ("Logger_file.txt")
Fh.setlevel (logging. ERROR)
Ch=logging. Streamhandler ()
Ch.setlevel (logging. CRITICAL)
Formater=logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ')
Fh.setformatter (Formater)
Ch.setformatter (Formater)
Logger_obj.addhandler (FH)
Logger_obj.addhandler (CH)
#logger_obj. SetLevel (logging. DEBUG)
return logger_obj
Logger_obj=get_logger ()
Logger_obj.info ( "info")
Logger_obj.error (" "Error")
Logger_obj.warning ( "warning")
Logger_obj.debug ( "debug")
Logger_obj.critical (" critical ")
======================
JSON serialization
Import JSON
--------------------Serialization of
dic={' Name ':' Wuhao ',"Age":32}
f=Open"Json_data2.txt","W") #创建一个文件把信息写到文件里
Way One
Data=json.dumps (DIC) #json. Dumps is called to serialize the object
Print (data)
Print (type (data))
F.write (data) #写到文件里
Way Two
Json.dump (Dic,f) # 1,data=json.dumps (DIC) 2, f.write (data) #做了两个事情
F.close ()
 
----deserialization of Json.loads
JSON represents a subset of objects in the standard JavaScript language, and the JSON and Python built-in data types correspond to the following:
===== Note that the JSON format must be "'" double quotes, otherwise it is not standard serialization, in Python, single double quotes do not matter
Pickle Module
##----------------------------SerializationImportPickle dic={‘Name‘:‘Alvin‘,‘Age': 23,‘Sex‘:‘Male‘}Print (Type (DIC))#<class ' Dict ' >j=Pickle.dumps (DIC)Print (Type (j))#<class ' bytes ' >F=open (‘Serialized Object _pickle‘,‘Wb‘)# Note is that W is written STR,WB is write Bytes,j is ' bytes ' F.write (j) #-------------------equivalent to Pickle.dump (dic,f) F.close () #-------------------------deserialization import< Span style= "COLOR: #000000" > Picklef=open ( " serialized object _pickle ", " rb '
Shelve module
The shelve module is simpler than the Pickle module, with only one open function, which returns a dictionary-like object, readable and writable; key must be a string, and the value can be a data type supported by Python
123456789101112 |
import shelve
f
= shelve.
open
(r
‘shelve.txt‘
)
# f[‘stu1_info‘]={‘name‘:‘alex‘,‘age‘:‘18‘}
# f[‘stu2_info‘]={‘name‘:‘alvin‘,‘age‘:‘20‘}
# f[‘school_info‘]={‘website‘:‘oldboyedu.com‘,‘city‘:‘beijing‘}
#
#
# f.close()
print
(f.get(
‘stu_info‘
)[
‘age‘
])
|
Module json,sys,pickle,logging