Last active
August 17, 2020 07:17
-
-
Save bregman-arie/2ba960ed135e71b1b8a0abdbfbff5086 to your computer and use it in GitHub Desktop.
Convert INI string to JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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