简体   繁体   English

使用PyYaml将集合转储到YAML文件

[英]Dumping Collection to YAML file with PyYaml

I am writing a python application. 我正在编写一个python应用程序。 I am trying to dump my python object into yaml using PyYaml. 我正在尝试使用PyYaml将python对象转储到yaml中。 I am using Python 2.6 and running Ubuntu Lucid 10.04. 我正在使用Python 2.6并运行Ubuntu Lucid 10.04。 I am using the PyYAML package in Ubuntu Package: http://packages.ubuntu.com/lucid/python/python-yaml 我正在Ubuntu软件包中使用PyYAML软件包: http : //packages.ubuntu.com/lucid/python/python-yaml

My object has 3 text variables and a list of objects. 我的对象有3个文本变量和一个对象列表。 Roughly it is something like this: 大概是这样的:

ClassToDump:

   #3 text variables
   text_variable_1
   text_variable_2
   text_variable_3
   #a list of AnotherObjectsClass instances
   list_of_another_objects = [object1,object2,object3]


AnotherObjectsClass:
   text_variable_1
   text_variable_2
   text_variable_3

The class that I want to dump contains a list of AnotherObjectClass instances. 我要转储的类包含AnotherObjectClass实例的列表。 This class has a few text variables. 此类具有一些文本变量。

PyYaml somehow does not dump the collections in AnotherObjectClass instance. PyYaml不会以某种方式将集合转储到AnotherObjectClass实例中。 PyYAML does dump text_variable_1, text_variable_2, and text_variable_3. PyYAML确实会转储text_variable_1,text_variable_2和text_variable_3。

I am using the following pyYaml API to dump ClassToDump instance: 我正在使用以下pyYaml API转储ClassToDump实例:

classToDump = ClassToDump();
yaml.dump(ClassToDump,yaml_file_to_dump)

Does any one has any experience with dumping a list of objects into YAML ? 有没有人有将对象列表转储到YAML的经验?

Here is the actual full code snippet: 这是实际的完整代码段:

def write_config(file_path,class_to_dump):    
    config_file = open(file_path,'w');  
    yaml.dump(class_to_dump,config_file);

def dump_objects():

rule = Miranda.Rule();
rule.rule_condition = Miranda.ALL
rule.rule_setting = ruleSetting
rule.rule_subjects.append(rule1)
rule.rule_subjects.append(rule2)
rule.rule_verb = ruleVerb

write_config(rule ,'./config.yaml');

This is the output : 这是输出:

!!python/object:Miranda.Rule rule_condition: ALL rule_setting: !!python/object:Miranda.RuleSetting {confirm_action: true, description: My Configuration, enabled: true, recursive: true, source_folder: source_folder} rule_verb: !!python/object:Miranda.RuleVerb {compression: true, dest_folder: /home/zainul/Downloads, type: Move File} !! python / object:Miranda.Rule规则条件:所有规则设置:!! python / object:Miranda.RuleSetting {confirm_action:true,描述:我的配置,启用:true,递归:true,source_folder:source_folder} rule_verb:!! python /object:Miranda.RuleVerb {压缩:true,dest_folder:/ home / zainul / Downloads,键入:Move File}

The PyYaml module takes care of the details for you, hopefully the following snippet will help PyYaml模块会为您处理详细信息,希望以下代码段对您有所帮助

import sys
import yaml

class AnotherClass:
    def __init__(self):
        pass

class MyClass:
    def __init__(self):
        self.text_variable_1 = 'hello'
        self.text_variable_2 = 'world'
        self.text_variable_3 = 'foobar'
        self.list_of_another_objects = [
            AnotherClass(),
            AnotherClass(),
            AnotherClass()
        ]

obj = MyClass()
yaml.dump(obj, sys.stdout)

The output of that code is: 该代码的输出为:

!!python/object:__main__.MyClass
list_of_another_objects:
- !!python/object:__main__.AnotherClass {}
- !!python/object:__main__.AnotherClass {}
- !!python/object:__main__.AnotherClass {}
text_variable_1: hello
text_variable_2: world
text_variable_3: foobar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM