pythonpython-3.xpyml

How to write yml file using python pyml package?


i want create yml file using python dictionary how to make dictionary format that i can get below format yml file

responses:
  utter_greet:
  - text: Hey! How are you?
    buttons:
    - title: "good"
      payload: "/greet"
    - title: "bad"
      payload: "/health"


Solution

  • You can use this package to convert to dict https://github.com/Infinidat/munch

    pip3 install munch
    

    convert to dict

    import yaml
    from munch import Munch
    mydict = yaml.safe_load("""
    responses:
      utter_greet:
      - text: Hey! How are you?
        buttons:
        - title: "good"
          payload: "/greet"
        - title: "bad"
          payload: "/health"
    """)
    print(mydict)
    

    convert dict to yaml

    with open('output.yml', 'w') as yaml_file:
        yaml.dump(mydict, yaml_file, default_flow_style=False)