Skip to content

Instantly share code, notes, and snippets.

@bregman-arie
Last active August 17, 2020 07:17
Show Gist options
  • Save bregman-arie/2ba960ed135e71b1b8a0abdbfbff5086 to your computer and use it in GitHub Desktop.
Save bregman-arie/2ba960ed135e71b1b8a0abdbfbff5086 to your computer and use it in GitHub Desktop.
Convert INI string to JSON
from collections import defaultdict
import configparser
def ini_to_json(ini):
"""Convert ini string to a dictionary and return it."""
config = configparser.ConfigParser(default_section=None,
interpolation=None)
config.read_string(ini)
config_dict = defaultdict(dict)
for section in config.sections():
for key, value in config.items(section):
config_dict[section][key] = value
return config_dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment