The configuration file is read in JSON mode

Source: Internet
Author: User

JSON file
{  "MySQL":{    "hosts":"localhost",    "User":"Root",    "Password":"Admin",    "Port":3306,    "dbname":"JFG",    "Chart":"Utf-8"},  "Redis":{    "Host":"localhost",    "User":"test1",    "Password":"test123test",    "Port":"6379"  }}
The use of JSON in Python
ImportJsonload Read the JSON file in the file loads convert the JSON string into a dictionary jump writes a string from the file to a JSON file jumps converts the dictionary to a string Cur_path= Os.path.dirname (Os.path.realpath (__file__))#The path under the current folder is more convenient. No need to write path to deathPrintCur_path#use of load in JSON#The first of these methodsWith open (' C:\peizhi.json ','R') as F:db=json.load (f)#second method (do not write dead path)With open (Cur_path +"\\"+"Peizhi",'R') as F:db=json.load (f) Usage of jumps a= {    "a":"123",    "b":"456"}b=Json.jumps (a)PrintB#The value of the JSON is printed outPrintType (b)#look at the type of JSONJumpwith Open ("'C:\test.json","W") as D:Check = Json.dump (a)#write in JSON


The content that is read in the configuration file. Display in a static manner
ImportOs,json,loggerclassdb_confg ():" "read the JSON configuration file for MySQL" "Cur_path= Os.path.dirname (Os.path.realpath (__file__))    Try:        if  notOs.path.exists (Cur_path +"\\"+"Peizhi"): Log.info ("Missing configuration file") with open (Cur_path+"\\"+"Peizhi",'R') as F:db=json.load (f)exceptException as E:Print "Error%s"%e @staticmethod#Static Methods    defuser_sql ():returndb_confg.db["MySQL"]["User"] @staticmethoddefpassword_sql ():returndb_confg.db["MySQL"]["Password"] @staticmethoddefhost_sql ():returndb_confg.db["MySQL"]["hosts"] @staticmethoddefport_sql ():returndb_confg.db["MySQL"]["Port"] @staticmethoddefdbname_sql ():returndb_confg.db["MySQL"]["dbname"] @staticmethoddefcharset_sql ():returndb_confg.db["MySQL"]["Chart"]
ImportMySQLdb fromComm.mod_configImport*classMysqldbhelper:#Get database connection
#获取数据库的连接中, there are a number of parameters that need to be written. Write each time manually. Very troublesome to put. It is convenient to change it directly from the configuration file. Although it is inconvenient, the address of the general database will not be changed.
defGetcon (self):Try: Conn=mysqldb.connect (host=db_confg.host_sql (), User=db_confg.user_sql (), passwd=db_confg.password_sql (), DB=Db_confg.dbname_sql (), Port=Db_confg.port_sql (), CharSet=db_confg.charset_sql ())returnConnexceptmysqldb.error,e:Print "MySQLdb error:%s"% E

Epilogue: Why use so many static methods. Just for MySQL. Do not need to write long strings of things;

The configuration file is read in JSON mode

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.