0 votes
1 view
in Python by (47.8k points)

How can I parse a YAML file in Python?

1 Answer

0 votes
by (107k 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 (9.3k points)
0 votes
1 answer
asked Jul 3, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...