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

发布于 2024-09-09 08:19:44 字数 1652 浏览 1 评论 0原文

我正在编写一个 python 应用程序。我正在尝试使用 PyYaml 将 python 对象转储到 yaml 中。我正在使用 Python 2.6 并运行 Ubuntu Lucid 10.04。我在 Ubuntu 包中使用 PyYAML 包: http://packages.ubuntu.com/ lucid/python/python-yaml

我的对象有 3 个文本变量和一个对象列表。大致是这样的:

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

我想要转储的类包含 AnotherObjectClass 实例的列表。这个类有一些文本变量。

PyYaml 不知何故不会转储 AnotherObjectClass 实例中的集合。 PyYAML 确实转储 text_variable_1、text_variable_2 和 text_variable_3。

我正在使用以下 pyYaml API 转储 ClassToDump 实例:

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

有人有将对象列表转储到 YAML 的经验吗?

这是实际的完整代码片段:

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');

这是输出:

!!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, 类型:移动文件}

I am writing a python application. I am trying to dump my python object into yaml using PyYaml. I am using Python 2.6 and running Ubuntu Lucid 10.04. I am using the PyYAML package in Ubuntu Package: http://packages.ubuntu.com/lucid/python/python-yaml

My object has 3 text variables and a list of objects. 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. This class has a few text variables.

PyYaml somehow does not dump the collections in AnotherObjectClass instance. PyYAML does dump text_variable_1, text_variable_2, and text_variable_3.

I am using the following pyYaml API to dump ClassToDump instance:

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

Does any one has any experience with dumping a list of objects into 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}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汹涌人海 2024-09-16 08:19:44

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)

该代码的输出是:

!!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

The PyYaml module takes care of the details for you, hopefully the following snippet will help

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文