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
  Replace values in Yaml file with value in dictionary PelleH 1 2,091 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  Best way to feed python script of a file absolut 6 1,062 Jan-11-2025, 07:03 AM
Last Post: Gribouillis
  Reading an ASCII text file and parsing data... oradba4u 2 1,393 Jun-08-2024, 12:41 AM
Last Post: oradba4u
Question Using Lists as Dictionary Values bfallert 8 2,175 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  need to compare 2 values in a nested dictionary jss 2 1,673 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  save values permanently in python (perhaps not in a text file)? flash77 8 2,650 Jul-07-2023, 05:44 PM
Last Post: flash77
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 7,924 Jun-29-2023, 11:57 AM
Last Post: gologica
  Python Script to convert Json to CSV file chvsnarayana 8 4,667 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
Video doing data treatment on a file import-parsing a variable EmBeck87 15 5,549 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Printing specific values out from a dictionary mcoliver88 6 2,741 Apr-12-2023, 08:10 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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