Python Yaml Sample Code

Dec 22, 2018

Install PyYAML.

pip install pyyaml

Parse yaml

import yamlyaml_config = '''user:  name: Desmond  number: 7  fav_language:    - Python    - Kotlin  friends: ['Mei Ru', 'Jackie', 'CJ']'''data = yaml.load(yaml_config)name = data['user']['name']

Write/Output yaml.

print(yaml.dump(data, default_flow_style=False))
user:
  fav_language:
  - Python
  - Kotlin
  friends:
  - Mei Ru
  - Jackie
  - CJ
  name: Desmond
  number: 7
print(yaml.dump(data, default_flow_style=True))
{user: {fav_language: [Python, Kotlin], friends: [Mei Ru, Jackie, CJ], name: Desmond,
    number: 7}}
print(yaml.dump(data))
user:
  fav_language: [Python, Kotlin]
  friends: [Mei Ru, Jackie, CJ]
  name: Desmond
  number: 7

NOTE: Refer Python Yaml Read and Write File

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.