-2

using python 3.10. My goal is to check wether a value from a list exists in a yaml file.

import yaml

versions_list= ["0.1.0-3572c21", "0.1.1", "0.0.2"]

with open('versions.yaml', 'r') as file:
    catalog = yaml.safe_load(file)
                
for version in versions_list:    
    for item, doc in catalog.items():
        if version in doc:
          print ("found!!!")
        else:
          print ("not found!!!")

Output is:

not found!!!
not found!!!
not found!!!

I expect to get:

found!!!
not found!!!
not found!!!

versions.yaml:

services:
  - name: svc1
    version: 0.1.0-3572c21
  - name: svc2
    revision: 0.0.1
  - name: svc3
    revision: 1.0.0
  - name: svc4
    revision: 2.0.1
jrz
  • 1,213
  • 4
  • 20
  • 54
  • 1
    I would exploring the data structures a little. For example, print out `doc` in the loop and see what it is. – happymacaron Jun 06 '22 at 15:27
  • 1
    Asking `if version in doc` when `doc` is a list means asking if any of the elements of the list are *equal* to `version`. – Scott Hunter Jun 06 '22 at 15:28
  • When you inspected or printed `item` and `doc` during execution, were they what you expected? If you are using an IDE **now** is a good time to learn its debugging features . [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). [How to step through Python code to help debug issues?](https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues). – wwii Jun 06 '22 at 16:25

1 Answers1

0
import yaml

versions_list= ["0.1.0-3572c21", "0.1.1", "0.0.2"]
with open('versions.yaml', 'r') as file:
  catalog = yaml.safe_load(file)
for item, doc in catalog.items():
  for key in doc:
    if any([key[data] in versions_list for data in key]):
      print ("found!!!")
    else:
      print ("not found!!!")
SCcagg5
  • 576
  • 3
  • 15