PyYaml Tutorial: How to read, write, modify, update yaml/yml files in Python || Python Yaml Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
to install pi yaml we just have to run pip install paiyama to import the library we have to use import yaml first let's take a look at our yaml files the first file one.yaml only contains one block of yaml data the second file multiple.yaml contains two block of yaml data so let's look at how to read a yaml file to read a yaml file we first have to open the file in read mode then load the contents using safe load there are multiple loaders because of different constructors it's best to stick with the safe version whenever possible because the safe load version will not create any arbitrary python objects now let's check what we have loaded and also let's check the data type that we have loaded so as you can see the contents loaded from our file is stored in a dictionary next let's look at how we can load a yaml file with multiple yaml data as usual we first have to open our file in read mode before we can load all the contents within now as you can see if we try to use safe load on our multiple.yaml file which has multiple yaml blocks of data it throws an error so we have to use a different function which is yaml.safe.o if we try to print this out we will get a generator object to see what we actually loaded we can simply change the object into a list now let's look at how we can modify the contents that we have loaded to modify the contents that we loaded we must first identify the data type if the value for the key is a string we must first put all the additional values in a list before we can update the key value pair for example let's update this platform value and we want to add facebook together with youtube since this is a dictionary we have to specify the key that we are trying to modify so i'm trying to update the platform key to point to the values youtube well as facebook we can see that the value for our platform key has been updated to youtube and facebook now let's try to update this video key and add an additional value to the list since the value is a list we can easily update it using a pen now let's run this again and as we can see the list value for the video key has been updated now let's look at how we can write the changes into a new file first we create a new file i'm going to call mine output.yaml we open it in write mode finally we use yamo.dump followed by the data which is contents and our output file which is dump file as we run this we can see that our output file is generated and the new values are updated within this yaml file so there you go thank you guys for watching if you found this video helpful don't forget to hit like and subscribe
Info
Channel: CK Codes
Views: 6,971
Rating: undefined out of 5
Keywords: yaml tutorial, yaml tutorial for beginners, python yaml, pyyaml tutorial, how to read write yaml file in python, how to modify yaml file in python, how to load yaml file in python, python yaml tutorial, python yaml automation, python yaml parser, python yaml parser tutorial, python read yaml file, python write yaml file, python modify yaml file, python get yaml file contents, python load yaml file, python yaml file, python yaml file tutorial, python yaml automation tutorial
Id: EhGcEyd1lrs
Channel Id: undefined
Length: 4min 43sec (283 seconds)
Published: Sat Aug 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.