使用Python编辑YAML文件时需要帮助
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应尝试更新或附加到现有的YAML文件。那是
非常难以正确。
而是加载数据,更新数据
并倾倒输出。如果您使用
ruamel.yaml
块/流风格将是保留和新元素默认为块风格:
它给出:
如果您不再想要“旧”
供应商
,则可以做:如果您尝试使用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 bepreserved and new elements default to block-style:
which gives:
If you no longer want the "old"
Vendor
you can e.g. do:If you try to do this with PyYAML you'll lose the comments.