【发布时间】:2019-02-20 13:50:27
【问题描述】:
我在 Python 中有这段代码,给定一个字典,它将 key:value 字段写入 config.ini 文件。问题是它一直在为每个字段编写标题。
import configparser
myDict = {'hello': 'world', 'hi': 'space'}
def createConfig(myDict):
config = configparser.ConfigParser()
# the string below is used to define the .ini header/title
config["myHeader"] = {}
with open('myIniFile.ini', 'w') as configfile:
for key, value in myDict.items():
config["myHeader"] = {key: value}
config.write(configfile)
这是.ini文件的输出:
[myDict]
hello = world
[myDict]
hi = space
我怎样才能摆脱双重标题[myDict]并得到这样的结果
[myDict]
hello = world
hi = space
?
在 Python 中创建 .ini 的代码取自 this question。
【问题讨论】:
标签: python dictionary configparser