如何将数据附加到YAML fi

2024-05-20 02:44:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文件*.yaml,其内容如下:

bugs_tree:
  bug_1:
    html_arch: filepath
    moved_by: user1
    moved_date: '2018-01-30'
    sfx_id: '1'

我想在这个文件的节点下添加一个新的子元素[bugs_tree] 我试着这样做:

if __name__ == "__main__":
    new_yaml_data_dict = {
        'bug_2': {
            'sfx_id': '2', 
            'moved_by': 'user2', 
            'moved_date': '2018-01-30', 
            'html_arch': 'filepath'
        }
    }

    with open('bugs.yaml','r') as yamlfile:
        cur_yaml = yaml.load(yamlfile)
        cur_yaml.extend(new_yaml_data_dict)
        print(cur_yaml)

那么文件应该是:

bugs_tree:
  bug_1:
    html_arch: filepath
    moved_by: username
    moved_date: '2018-01-30'
    sfx_id: '1234'
  bug_2:
    html_arch: filepath
    moved_by: user2
    moved_date: '2018-01-30'
    sfx_id: '2'

当我试图执行.append().extend().insert()时,得到错误

cur_yaml.extend(new_yaml_data_dict)
AttributeError: 'dict' object has no attribute 'extend'

Tags: 文件idyamldatebyhtmlbugdict
3条回答

你需要使用^{}

cur_yaml.update(new_yaml_data_dict)

结果代码

with open('bugs.yaml','r') as yamlfile:
        cur_yaml = yaml.load(yamlfile)
        cur_yaml.update(new_yaml_data_dict)
        print(cur_yaml)

with open('bugs.yaml','w') as yamlfile:
        yaml.safe_dump(cur_yaml, yamlfile) # Also note the safe_dump

如果你想更新文件,一次读取是不够的。 您还需要针对该文件再次写入。 像这样的方法会奏效:

with open('bugs.yaml','r') as yamlfile:
    cur_yaml = yaml.safe_load(yamlfile) # Note the safe_load
    cur_yaml['bugs_tree'].update(new_yaml_data_dict)

if cur_yaml:
    with open('bugs.yaml','w') as yamlfile:
        yaml.safe_dump(cur_yaml, yamlfile) # Also note the safe_dump

我没有对此进行测试,但他认为可以使用read读取文件,使用write写入文件。使用safe_loadsafe_dump就像Anthon所说:

"There is absolutely no need to use load(), which is documented to be unsafe. Use safe_load() instead"

不确定这是否适合每个人的用例,但我发现你可以。。。如果文件只包含顶级列表,则追加到该文件中。

做这件事的一个动机是它只是有意义。另一个原因是我怀疑每次都要重新加载和解析整个yaml文件。我想做的是使用Django中间件来记录传入的请求,以调试我在开发过程中遇到的多个页面加载的错误,这是相当关键的时间。

如果我必须按照OP的要求做,我会考虑把bug放在它们自己的文件中,并从中合成bugs_tree的内容。

import os
import yaml
def write(new_yaml_data_dict):

    if not os.path.isfile("bugs.yaml"):

        with open("bugs.yaml", "a") as fo:
            fo.write("---\n")

    #the leading spaces and indent=4 are key here!
    sdump = "  " + yaml.dump(
                new_yaml_data_dict
                ,indent=4
                )

    with open("bugs.yaml", "a") as fo:
        fo.write(sdump)

new_yaml_data_dict = {
        'bug_1': {
            'sfx_id': '1', 
            'moved_by': 'user2', 
            'moved_date': '2018-01-20', 
            'html_arch': 'filepath'
        }
    }
write(new_yaml_data_dict)
new_yaml_data_dict = {
        'bug_2': {
            'sfx_id': '2', 
            'moved_by': 'user2', 
            'moved_date': '2018-01-30', 
            'html_arch': 'filepath'
        }
    }
write(new_yaml_data_dict)

结果是

---
  bug_1:
    html_arch: filepath
    moved_by: user2
    moved_date: '2018-01-20'
    sfx_id: '1'
  bug_2:
    html_arch: filepath
    moved_by: user2
    moved_date: '2018-01-30'
    sfx_id: '2'

相关问题 更多 >