如何在没有 configparser 的情况下读取 Python 中的 INI 文件?

How can I read INI file in Python without configparser?

我有 INI 文件,它包含下一个:

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

所以基本上在 ini 文件中我有 json。如果不使用 configparser ,我找不到使用文件中的键的方法。请帮助

您是否尝试过将其作为 json 文件打开?由于 ini 文件是一个文本文件,它应该 * 可以正常工作 *

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)

您的 ini 文件的内容将在 data 中,这将是一个字典。