简体   繁体   中英

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.

---
foo: /bar

I am using 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.

[foobar]
foo = /bar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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