Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

How can I parse a YAML file in Python?

1 Answer

0 votes
by (106k points)

The easiest and purest method for parsing a YAML file in Python is as follows:-

#!/usr/bin/env python

import yaml

with open("example.yaml", 'r') as stream: 

      try:

         print(yaml.safe_load(stream))

      except yaml.YAMLError as exc:

         print(exc) 

There is a plain yaml.load() function that also exists, but yaml.safe_load() should always be preferred unless you explicitly need the arbitrary object serialization/deserialization provided in order to avoid introducing the possibility for arbitrary code execution.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Dec 18, 2020 in Python by laddulakshana (16.4k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.6k answers

500 comments

108k users

Browse Categories

...