I need to loop through the main tag and fetch its subtag value in python scrip.
Sorry i am recently started using python script.
Chart1:
Slide: 1
Top: 100
Left: 100
Width: 150
Heigth: 150
Chart2:
Slide: 1
Top: 100
Left: 100
Width: 150
Heigth: 150
Chart3:
Slide: 1
Top: 100
Left: 100
Width: 150
Heigth: 150
import yaml
from munch import Munch
#Read Yaml file
with open(r"C:\Users\ana\Chart\config.yaml", 'r') as stream:
data_loaded = yaml.safe_load(stream)
configyaml = Munch(data_loaded)
print(data_loaded)
for index in range(3):
Slidenumber = configyaml.Chart + index +.Slide
STop = configyaml.Chart + index +.Top
Sleft= configyaml.Chart + index +.Left
print(Slidenumber)
print(STop)
print(Sleft)
You are trying to read a yaml file contaning three charts, and then loop through each chart to fetch specific values and print them out. However, there are a few issues in your code that are preventing the values from being displayed correctly.
for index in range(1, 4):
chart_data = configyaml.get(f"Chart{index}")
if chart_data:
Slidenumber = char_data.get('Slide')
STop = chart_data.get('Top')
SLeft = chart_data.get('Left')
print(f"Chart {index}: Slide={Slidenumber}, Top={STop}, Left={Sleft}")
With these modifications, the code should work correctly, allowing you to loop through each chart, retrieve the desired values, and print them as intended.