使用Python编辑YAML文件时需要帮助

发布于 2025-01-24 01:12:18 字数 1784 浏览 3 评论 0原文

我正在尝试使用Python编辑YAML文件。我正在使用Python的Pyyaml模块来做到这一点。 yaml文件:

version: '1.0'
Fruits:
####################Place Index##################
  #Vendor:
  Vendor:
    location: France
    city: paris
    customers:
     - "Lucy"
    Type:
     Apple : "Red"
     Watermelon : "Small"

我想更改“水果”下“类型”中的“西瓜”的值,然后添加一个新的new_vendor。为此,我正在使用此代码:

#! /usr/bin/python
#! /usr/bin/python3

import yaml
import string
import sys
#import ruamel.yaml

#yaml = ruamel.yaml.YAML()

new_vendor = {
  'New_Vendor':
  {
   'location': 'France',
   'city': 'Nice',
   'Type':
   {
     'Apple' : 'Green',
     'Watermelon' : 'Medium',
     'Pumpkin' : 'Large'
   },
   'customers':
   [
      'Lucy',
      'David'
   ],
  }
          }


with open("compose.yaml", "r+") as comp:
    #data = yaml.load(comp, Loader=yaml.FullLoader)
    data = yaml.load(comp)
    data["Fruits"]["Vendor"]["Type"]["Watermelon"] = "Medium"
    #yaml.dump(license, comp, sort_keys=False)
    yaml.dump(new_vendor, comp)
    yaml.dump(data, comp)
    #yaml.dump_all(data, comp, default_flow_style=False)

并且,执行此代码后,我将获得此结果:

version: '1.0'
Fruits:
####################Place Index##################
  #Vendor:
  Vendor:
    location: France
    city: paris
    customers:
     - "Lucy"
    Type:
     Apple : "Red"
     Watermelon : "Small"
New_Vendor:
  Type: {Apple: Green, Pumpkin: Large, Watermelon: Medium}
  city: Nice
  customers: [Lucy, David]
  location: France
Fruits:
  Vendor:
    Type: {Apple: Red, Watermelon: Medium}
    city: paris
    customers: [Lucy]
    location: France
version: '1.0'

但这不是我想要的输出。首先,第一个参数“供应商”正在重复自我,而“西瓜”值的更改正在重复参数中发生,而不是原始参数中的重复参数。另外,我不希望用{}[]编写字典和数组。

如何解决这个问题?

I am trying to edit YAML file using python. I am using PyYAML module of python to do this.
The YAML file:

version: '1.0'
Fruits:
####################Place Index##################
  #Vendor:
  Vendor:
    location: France
    city: paris
    customers:
     - "Lucy"
    Type:
     Apple : "Red"
     Watermelon : "Small"

I want change the value of "Watermelon" in "Type" under "Fruits" and add a new new_vendor. To do this I am using this code:

#! /usr/bin/python
#! /usr/bin/python3

import yaml
import string
import sys
#import ruamel.yaml

#yaml = ruamel.yaml.YAML()

new_vendor = {
  'New_Vendor':
  {
   'location': 'France',
   'city': 'Nice',
   'Type':
   {
     'Apple' : 'Green',
     'Watermelon' : 'Medium',
     'Pumpkin' : 'Large'
   },
   'customers':
   [
      'Lucy',
      'David'
   ],
  }
          }


with open("compose.yaml", "r+") as comp:
    #data = yaml.load(comp, Loader=yaml.FullLoader)
    data = yaml.load(comp)
    data["Fruits"]["Vendor"]["Type"]["Watermelon"] = "Medium"
    #yaml.dump(license, comp, sort_keys=False)
    yaml.dump(new_vendor, comp)
    yaml.dump(data, comp)
    #yaml.dump_all(data, comp, default_flow_style=False)

And, after executing this code I am getting this result:

version: '1.0'
Fruits:
####################Place Index##################
  #Vendor:
  Vendor:
    location: France
    city: paris
    customers:
     - "Lucy"
    Type:
     Apple : "Red"
     Watermelon : "Small"
New_Vendor:
  Type: {Apple: Green, Pumpkin: Large, Watermelon: Medium}
  city: Nice
  customers: [Lucy, David]
  location: France
Fruits:
  Vendor:
    Type: {Apple: Red, Watermelon: Medium}
    city: paris
    customers: [Lucy]
    location: France
version: '1.0'

But this is not the output I want. Firstly, the first parameter "Vendor" is repeating itself and the change of "Watermelon" value is happening in the repeated parameters not in the original parameters. Also I don't want dictionary and array to be written with {} and [].

How can I resolve this issue?

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

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

发布评论

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

评论(1

蘸点软妹酱 2025-01-31 01:12:18

您不应尝试更新或附加到现有的YAML文件。那是
非常难以正确。

而是加载数据,更新数据
并倾倒输出。如果您使用ruamel.yaml块/流风格将是
保留和新元素默认为块风格:

import sys
import ruamel.yaml
from pathlib import Path

    
yaml = ruamel.yaml.YAML()
yaml.indent(sequence=3, offset=1)
yaml.preserve_quotes = True

file_in = Path('compose.yaml')

new_vendor = {
  'New_Vendor':
  {
   'location': 'France',
   'city': 'Nice',
   'Type':
   {
     'Apple' : 'Green',
     'Watermelon' : 'Medium',
     'Pumpkin' : 'Large'
   },
   'customers':
   [
      'Lucy',
      'David'
   ],
  }
}

data = yaml.load(file_in)
data['Fruits'].update(new_vendor)
yaml.dump(data, sys.stdout)

它给出:

version: '1.0'
Fruits:
####################Place Index##################
  #Vendor:
  Vendor:
    location: France
    city: paris
    customers:
     - "Lucy"
    Type:
      Apple: "Red"
      Watermelon: "Small"
  New_Vendor:
    location: France
    city: Nice
    Type:
      Apple: Green
      Watermelon: Medium
      Pumpkin: Large
    customers:
     - Lucy
     - David

如果您不再想要“旧” 供应商,则可以做:

del data['Fruits']['Vendor']

如果您尝试使用Pyyaml进行此操作,则会丢失评论。

You should not try to update, or append to an existing YAML file. That is
extremely difficult to get right.

Instead load the data, update the data
and dump the output. If you do so with ruamel.yaml the block/flow-style will be
preserved and new elements default to block-style:

import sys
import ruamel.yaml
from pathlib import Path

    
yaml = ruamel.yaml.YAML()
yaml.indent(sequence=3, offset=1)
yaml.preserve_quotes = True

file_in = Path('compose.yaml')

new_vendor = {
  'New_Vendor':
  {
   'location': 'France',
   'city': 'Nice',
   'Type':
   {
     'Apple' : 'Green',
     'Watermelon' : 'Medium',
     'Pumpkin' : 'Large'
   },
   'customers':
   [
      'Lucy',
      'David'
   ],
  }
}

data = yaml.load(file_in)
data['Fruits'].update(new_vendor)
yaml.dump(data, sys.stdout)

which gives:

version: '1.0'
Fruits:
####################Place Index##################
  #Vendor:
  Vendor:
    location: France
    city: paris
    customers:
     - "Lucy"
    Type:
      Apple: "Red"
      Watermelon: "Small"
  New_Vendor:
    location: France
    city: Nice
    Type:
      Apple: Green
      Watermelon: Medium
      Pumpkin: Large
    customers:
     - Lucy
     - David

If you no longer want the "old" Vendor you can e.g. do:

del data['Fruits']['Vendor']

If you try to do this with PyYAML you'll lose the comments.

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