0

I am trying to get data from Yaml document and put it into a dictionary, and then reach certain values. I am having issues reaching these values at the moment, so I am think I may have messed up the conversion from yaml structure to dictionary. Can someone help me with this error?

When its just a yaml.dump its set up like this:

{10000: {Name: Jim, ID: 10000, Number: 435-323-4444},
 10001: {Name: Tara, ID: 10001, Number: 234-234-3333},
 10002: {Name: Lee, ID: 10002, Number: 213-324-5565}}

This is the code:

yaml_dict = dict()
with open(yaml_file.yaml, 'r') as file:
   try:
       yaml_dict = (yaml.dump(file),default_flow_style=True)
   except yaml.YAMLError as err:
       print(err)
yaml_dict = yaml_dict.todict('list')
print(yaml_dict[10000]['Name'])

The error I get is for this line: yaml_dict = yaml_dict.todict('list')

It reads: AttributeError: 'str' object has no attribute 'todict'

I am trying to get this result:

Jim
tejosa
  • 135
  • 2
  • 11

1 Answers1

1

You need to use yaml.load(file) instead of yaml.dump(file).

Please see an example here.

Greg
  • 1,845
  • 2
  • 16
  • 26