toml库不如tomlkit库

阅读 58

2023-11-09

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)

精彩评论(0)

0 0 举报