How can I parse a YAML file in Python?
Hi Shielagaa,
You can use PyYAML library. Here is the code below:
import yaml
with open('file.yaml', 'r') as file:
data = yaml.safe_load(file)
print(data)
You can use ruamel.yaml library (a YAML parser that preserves comments by default):
from ruamel.yaml import YAML
yaml = YAML()
with open('file.yaml', 'r') as file:
data = yaml.load(file)
print(data)
Hey, using the yaml module from the standard library (deprecated in Python 3.9 and removed in Python 3.10, so not recommended for new code).
import yaml
with open('file.yaml', 'r') as file:
data = yaml.load(file, Loader=yaml.FullLoader)
print(data)