简体   繁体   English

使用 python 将 yaml 文件转换为 ini 文件

[英]convert yaml file to ini file with python

I have this script:我有这个脚本:

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)

But when I run this I get this error:但是当我运行它时,我得到了这个错误:

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'

So what am I doing wrong?那么我做错了什么?

Here is what the yaml file looks like.这是 yaml 文件的样子。

---
foo: /bar

I am using Python 3.6.4我正在使用 Python 3.6.4

I needed to make up a section like so:我需要像这样组成一个部分:

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

So this is my resulting ini file.所以这是我生成的ini文件。

[foobar]
foo = /bar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM