【问题标题】:convert yaml file to ini file with python使用python将yaml文件转换为ini文件
【发布时间】:2020-04-27 16:17:38
【问题描述】:

我有这个脚本:

import yaml
import configparser


with open('./prd.yaml') as f:
    input_data = yaml.safe_load(f)

config = configparser.ConfigParser()
config.read_dict(input_data)

with open('./prd.ini', 'w') as configfile:
    config.write(configfile)

但是当我运行它时,我得到了这个错误:

Traceback (most recent call last):
  File "yaml-to-ini.py", line 9, in <module>
    config.read_dict(input_data)
  File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/configparser.py", line 747, in read_dict
    for key, value in keys.items():
AttributeError: 'str' object has no attribute 'items'

那我做错了什么?

这是 yaml 文件的样子。

---
foo: /bar

我正在使用 Python 3.6.4

【问题讨论】:

  • 你能在你的 yaml 中添加一个简短的 sn-p,就足以产生这个错误吗?
  • @usr2564301 即使是最简单的 yaml 也会发生这种情况。查看我更新的帖子。

标签: python yaml ini


【解决方案1】:

我需要像这样组成一个部分:

config.read_dict({'foobar': input_data})

这是我生成的ini文件。

[foobar]
foo = /bar

【讨论】:

    猜你喜欢
    • 2012-10-12
    • 2020-07-17
    • 2020-11-14
    • 1970-01-01
    • 2011-06-22
    • 2016-06-05
    • 2013-06-23
    • 2018-11-23
    • 1970-01-01
    相关资源
    最近更新 更多