Python Forum
Python Script for parsing dictionary values from yaml file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Script for parsing dictionary values from yaml file
#1
Requires Help ,using Python for accessing dictionary values from yaml file

Also , the yaml file will be passed as argument as INV file command line to execute playbook:

something like :

"ansible-playbook test.yml -e INV_FILE=latest123.yaml"


For Example - Parse the each key and extract the “product_name”, “release_candidate” and “version“ & Print results as list

The yaml file looks like



- releases:
- gotcha:
file_name: gtp.tar
md5: 11f6987275613ca3ecf832ea59c1
product_name: gotcha
product_number: GTP 8881
release_candidate: rc19
version: 19.0.0
- tp:
file_name: tpr.tar
md5: ebd7742ed8fff0472733c0cd8e
product_name: tp
product_number: TTP 8057
release_candidate: rc25
version: 25.0.0
- device_name:
file_name: dna.tar
md5: e464e69a07123de075778aa9b7
product_name: device_name
product_number: DNP 6746
release_candidate: rc25
version: 25.0.0
Reply
#2
So what have you tried?
If you want someone to write it for you, post under jobs and include information about compensation.
Reply
#3
What about PyYaml library?!
Reply
#4
Tried the following with no luck


import os
import yaml

def yaml_loader(filepath):
    """Loads a yaml file """
    with open(filepath, "r") as file_descriptor:
        data = yaml.load(file_descriptor)
    return data


if __name__ == "__main__":
    filepath = "latest123.yaml"
    data = yaml_loader(filepath)
    print (data)

    items = data.get('releases')
    for item,name, item_value in items.iteritems():
        print (item_name, item_value)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need to compare 2 values in a nested dictionary jss 2 798 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  save values permanently in python (perhaps not in a text file)? flash77 8 1,121 Jul-07-2023, 05:44 PM
Last Post: flash77
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,009 Jun-29-2023, 11:57 AM
Last Post: gologica
  Python Script to convert Json to CSV file chvsnarayana 8 2,346 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,669 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Printing specific values out from a dictionary mcoliver88 6 1,317 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 1,177 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  Trying to use 2 values from excel in my script but getting error.. cubangt 3 1,624 May-11-2022, 07:12 AM
Last Post: normanwolf
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,580 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Overwrite values in XML file with values from another XML file Paqqno 5 3,213 Apr-01-2022, 11:33 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020