0

I have below yml file and want to get Authority, Portfolio, ID, Parent, etc. in different variables. On top of this, I want a loop that will run over this element one by one. In this case for loop run will 2 times. each time with these values in variables.

I am able to read the file but how can I wrote a for loop and get all these values in variables.

with open(r'/configuration.yml') as file:
    Config = yaml.load(file, Loader=yaml.FullLoader)
    print(Config)
  1. Authority:ABC
  2. Portfolio:PR
  3. PortfolioID:1
  4. Parent:UU
  5. ProjectName:UU
  6. URLLogon:https://
  7. -
  8. Authority:ABC
  9. Portfolio:PR
  10. Parent:II
  11. ProjectName:MM
  12. URLLogon:https://
bereal
  • 32,519
  • 6
  • 58
  • 104
Andrew
  • 183
  • 2
  • 14
  • The specifics depends on your yaml file structure, but `Config` is either a list or a dict, that already has nothing to do with yaml, and you iterate over it just like you normally iterate over a list or a dict. – bereal Dec 03 '19 at 11:22
  • my YAML file structure is specified above, 1-6 elements repeats 20 times for diff config values – Andrew Dec 03 '19 at 11:25
  • So `Config` is a list of dictionaries. The problem is reduced to a known one. – bereal Dec 03 '19 at 11:33
  • Hope help you. https://stackoverflow.com/questions/1773805/how-can-i-parse-a-yaml-file-in-python – Dmitry Leiko Dec 03 '19 at 11:35
  • Probably, you don't need any loop. Consider using ready solution - PyYAML ( https://pyyaml.org/wiki/PyYAML ). It can parse your yaml file and help you to access all kind of data that are in the yaml file (even if nested). Use serialization to OrderedDict if order matters (and it looks this way in your case). – Maciek Dec 03 '19 at 12:03
  • Description of the YAML file's content is completely **unclear**. What are numbers? Are they top-level keys? Without understanding of your YAML structure we cannot help you. Please, add **exact** example of YAML file into the question post, you may format this example as a *code* (with `Ctrl+K` or `{}` button). – Tsyvarev Dec 03 '19 at 14:43

0 Answers0