Я новичок в python и хочу проанализировать config.yaml ниже
name: bank_csv_sample_yaml
description: "This yaml will export csv file to another csv file"
initialize:
- !DataSource
name: bank_source
description: "Source details"
dfid: bank_dataframe
type: flatfile
location:
filename: "bank1.csv"
format: csv
Вот мой код для python
import yaml
class DataSource(yaml.YAMLObject):
yaml_tag = '!DataSource'
def __init__(self, name, description, dfid, type, location, filename, format):
self.name = name
self.description = description
self.dfid = dfid
self.type = type
self.location = location
self.filename = filename
self.format = format
def __repr__(self):
return "%s(name:%r, description:%r, dfid:%r, type:%r, location:%r, filename:%r, format:%r)" % (self.__class__.__name__, self.name, self.description, self.dfid, self.type, self.location, self.filename, self.format)
if __name__ == "__main__":
with open("config.yaml", "r") as yaml_reader:
yaml_data = yaml.load(yaml_reader, Loader=yaml.FullLoader)
print(yaml_data)
И получил результат
{'name': 'bank_csv_sample_yaml', 'description': 'This yaml will export csv file to another csv file', 'initialize': [DataSource(name:'bank_source', description:'Source details', dfid:'bank_dataframe', type:'flatfile', location:None, filename:'bank1.csv', format:'csv')]}
Как мне преобразовать
[DataSource(name:'bank_source', description:'Source details', dfid:'bank_dataframe', type:'flatfile', location:None, filename:'bank1.csv', format:'csv')]
в словарь? Я не знаю, как разделить эти значения.
Ожидаемый вывод - это что-то вроде вложенного словаря.
{'name': 'bank_csv_sample_yaml', 'description': 'This yaml will export csv file to another csv file', 'initialize': {‘DataSource’ : {name: 'bank_source', description: 'Source details', dfid: 'bank_dataframe', type: 'flatfile', location: None, filename: 'bank1.csv', format: 'csv'}}}
Спасибо.