Search code examples
jsonpython-3.xreadfileini

How can I read INI file in Python without configparser?


I have INI file, it contains next:

{
   "input": {
      "json": "good.json",
      "csv": "good.csv",
      "encoding": "utf-8"
   },
   "output": {
      "fname": "res.txt",
      "encoding": "utf-8"
   }
}

so basically inside ini file I have json. I can no find a way to work with keys inside the file without using configparser . Please help


Solution

  • Have you tried just opening it as a json file? Since the ini file is a text file, it should * just work *

    import json
    
    # I saved your example in a text file called test.ini and 
    # put it in the same folder as this script
    with open('test.ini', 'r') as f:
        data = json.load(f)
    

    The contents of your ini file will be in data, which will be a dictionary.