[英]How to transform an object into a dictionary?
I have a return of an object <generator object load_all at 0x000000000309AB40>
from a function that opens and reads a YAML file. 我从打开并读取YAML文件的函数返回了对象
<generator object load_all at 0x000000000309AB40>
。 From this return, I need to create an organized file dictionary. 从此返回中,我需要创建一个有组织的文件字典。
#FILE YAML TEST.yaml
---
MAIN:
Name: Jaqueline
Age: 30
City: New York
OTHER:
State: True
______________________________________________________
#PYTHON
from ruamel.yaml import YAML
from pathlib import Path
def file_yaml():
yaml = YAML()
stream = Path('TEST.yaml')
yaml_conf=yaml.load_all(stream)
print(yaml_conf)
return yaml_conf
yaml=file_yaml()
dictionary=?????
yaml is an object <generator object load_all at 0x000000000309AB40>
yaml是一个对象
<generator object load_all at 0x000000000309AB40>
If I make: ... 如果我做...
yaml=file_yaml()
dictionary =dict(yaml)
print dictionary
I have: {'MAIN': 'OTHER'}
. 我有:
{'MAIN': 'OTHER'}
。 I lost a lot of information from the archive !!! 我从档案中丢失了很多信息! How can I create a correct dictionary with the data of the file?
如何使用文件数据创建正确的字典?
It works fine for me: 这对我来说可以:
import yaml
with open('random.yaml', 'r') as fin:
yml = yaml.safe_load(fin)
yml
{'MAIN': {'Name': 'Jaqueline', 'Age': 30, 'City': 'New York'},
'OTHER': {'State': True}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.