How can I convert the result of a ConfigParser.items('section') to a dictionary to format a string like here:
import ConfigParser config = ConfigParser.ConfigParser() config.read('conf.ini') connection_string = ("dbname='%(dbname)s' user='%(dbuser)s' host='%(host)s' " "password='%(password)s' port='%(port)s'") print connection_string % config.items('db')
Have you tried
print connection_string % dict(config.items('db'))
?
How I did it in just one line.
my_config_parser_dict = {s:dict(config.items(s)) for s in config.sections()}
No more than other answers but when it is not the real businesses of your method and you need it just in one place use less lines and take the power of dict comprehension could be useful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With