Reputation: 1
I have the below posted .ini file and i want to have access to its contents from .py and print the value of the api key
config.ini:
[API_KEY]
default = 5b3ce3597851110001cf62480ecf8c403567479a87de01df5da651cds
please let me know how to fix it
code:
import configparser
config = configparser.ConfigParser()
config.read('configs.ini')
print(config.sections())
print(config['API_KEY'])
exit()
output:
['API_KEY']
<Section: API_KEY>
Upvotes: 0
Views: 851
Reputation: 10960
Subscript after reading the config
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
print(config['API_KEY']['default'])
Output
'5b3ce3597851110001cf62480ecf8c403567479a87de01df5da651cds'
It would be much simpler to store it in a JSON.
JSON configuration example config.json
{
"api_key": {
"default": "XYZ",
"development": "XYZ",
"production": "ABC"
}
}
Code
import json
with open('config.json', 'r') as cfile:
data = json.load(cfile)
print(data.get('api_key').get('default')) # XYZ
Upvotes: 2