import toml
addr = r"d:\data\config.toml"
config = {'database': {'host': 'localhost', 'port': 5432}}
# dumps 将字典转换为TOML格式的字符串
toml_str = toml.dumps(config)
# 在字符串中添加注释
toml_str_with_comments = '# 这是一个注释\n' + toml_str
# write 将字符串写入文件
with open(addr, 'w') as f:
f.write(toml_str_with_comments)
标签:addr,不如,dumps,toml,str,字符串,tomlkit,config
From: https://blog.51cto.com/u_16055028/8284675