What's the official way of storing settings for python programs?

后端 未结 13 1358
傲寒
傲寒 2020-12-12 23:58

Django uses real Python files for settings, Trac uses a .ini file, and some other pieces of software uses XML files to hold this information.

Are one of these approa

13条回答
  •  别那么骄傲
    2020-12-13 00:07

    One of the easiest ways which is use is using the json module. Save the file in config.json with the details as shown below.

    Saving data in the json file:

    {
    
        "john"  : {
            "number" : "948075049" , 
            "password":"thisisit" 
        }    
    }
    

    Reading from json file:

    import json
    
    #open the config.json file 
    
    with open('config.json') as f:
        mydata = json.load(f) ; 
    
    #Now mydata is a python dictionary 
    
    print("username is " , mydata.get('john').get('number') , " password is " , mydata.get('john').get('password')) ;
    

提交回复
热议问题