Python Forum
get variable of yaml file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: get variable of yaml file (/thread-24639.html)



get variable of yaml file - lateublegende - Feb-24-2020

Hello, I have some problem with my program. I have try to solve my problem by myself but I do something wrong. Here my yaml file.

role.one.name: werewolf
role.one.desc: you are a werewolf.

role.two.name: villager
role.two.desc: you are a villager.
And this is my python code.
import yaml
with open(r'/myyamlfile') as file:
            role=yaml.load(file, Loader=yaml.FullLoader)

#selecting the role randomly

yourrole=role.one.get("name")

avatarname.set("role", yourrole)
But I get this error
Error:
yourrole=role.one.get("name") AttributeError: 'dict' object has no attribute 'one'
I try many thing, like switch the number into letter ( like in this exemple). I don't know what I doing wrong. thank you for your reply.


RE: get variable of yaml file - ndc85430 - Feb-25-2020

role appears to be a dict, given the error message. Perhaps you could at least try printing the keys in that dict? That would be a useful place to start. I imagine the docs for the module also tell you how to use it..