使用python ruamel.yaml将内容添加到yaml文件时,从dict值中删除单引号 [英] remove single quotes from dict values while adding content to yaml file using python ruamel.yaml
问题描述
我有一个yaml文件,如下所述 test1.yaml
I have an yaml file as mentioned below test1.yaml
resources:
name:{get_param: vname}
ssh_keypair: {get_param: ssh_keypair}
现在,我想在test1.yaml资源下添加test1_routable_net:{get_param:abc_routable_net} 这是我尝试过的代码
Now I want to add test1_routable_net: { get_param: abc_routable_net } under resources of test1.yaml Here is the code which I tried
import ruamel.yaml
yaml = ruamel.yaml.YAML()
test="{ get_param: abc_routable_net }".strip(‘\’’)
with open('/tmp/test1.yaml') as fp:
data = yaml.load(fp)
data['resources'].update({‘test1_routable_net’:test})
yaml.dump(data,file('/tes2.yaml', 'w'))
上面代码的
输出是 tes2.yaml
output of above code is tes2.yaml
resources:
name:{get_param: vname}
ssh_keypair: {get_param: ssh_keypair}
test1_routable_net: '{ get_param: abc_routable_net }'
所需的输出是
tes2.yaml
Desired output is
tes2.yaml
resources:
name:{get_param: vname}
ssh_keypair: {get_param: ssh_keypair}
test1_routable_net: { get_param: abc_routable_net }
我尝试使用test.strip('\'')
,但仍然没有用,我看到值的单引号....如何从值中删除那些引号?
I tried using test.strip('\'')
, but no use still I see single quotes for the value .... How can I remove those quotes from the value?
推荐答案
在您的程序中,test
是一个字符串.字符串通常在转储时不加引号,但是如果它们的解释会模棱两可,那么它们会被引用.这就是为什么您的输出在其周围加上单引号的原因:确保在读回该节点时不会被错误地解释为映射而不是字符串.
因此,用.strip()
删除不存在的引号不会执行任何操作.
In your program test
is a string. Strings normally don't get quoted when dumped, but if their interpretation would be ambiguous, they will be. That is the reason why your output has the single quotes around them: to make sure that on reading back in this node is not incorrectly interpreted as a mapping instead of a string.
Removing the non-existent quotes with .strip()
therefore doesn't do anything.
您应该从要完成的工作开始倒退(实际上,您需要映射而不是字符串,就像从输出中看到的那样).
You should work backwards from what you what you want to accomplish (you actually want a mapping instead of a string, as one can see from the output).
如果加载所需的输出,您将看到test1_routable_net
的值是python dict(或其子类),因此请确保这是您分配给test
的内容:
If you load your desired output, you will see that the value for test1_routable_net
is a python dict (or a subclass thereof), so make sure that is what you assign to test
:
import sys
import ruamel.yaml
yaml = ruamel.yaml.YAML()
test = { 'get_param': 'abc_routable_net' }
with open('./test1.yaml') as fp:
data = yaml.load(fp)
data['resources'].update({'test1_routable_net': test})
yaml.dump(data, sys.stdout)
哪个给:
resources:
name:{get_param: vname}
ssh_keypair: {get_param: ssh_keypair}
test1_routable_net:
get_param: abc_routable_net
从语义上讲,它与所需的输出相同,但是由于您希望以流样式显示get_param: abc_routable_net
,因此可以添加:
This is semantically the same as your desired output, but since you want the get_param: abc_routable_net
in flow-style, you could add:
yaml.default_flow_style=None
以获得所需的输出.您还可以查看将ruamel.comments.CommentedMap
分配给test
,这使您可以更精细地控制其样式(和注释等).
to get your desired output. You can also look at assigning, to test
, a ruamel.comments.CommentedMap
, which gives you more fine grained control over its style (and comments, etc.).
这篇关于使用python ruamel.yaml将内容添加到yaml文件时,从dict值中删除单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!