简体   繁体   English

如何通过Python在yaml文件中的一个标签下添加括号{}?

[英]How to add a bracket {} under one tag in yaml file by Python?

I want to make one YAML file by Python like as below.我想用 Python 制作一个 YAML 文件,如下所示。

A:
  B:
     c: {d:e}

but if I do it like as below:但如果我像下面那样做:

data = {'A':{'B':{ 'c':'{d:e}'}}}
yaml.dump(data,file,default_flow_style = False)

the output in the file is as below:文件中的输出如下:

A:
  B:
    c: '{d: e}'

I don't want the quotation around.我不想周围的报价。 How to do it?怎么做?

You made a string out of {d:e} by putting quotes around the whole thing.您通过在整个事物周围加上引号来从{d:e}创建一个字符串。 Change '{d:e}' to {'d':'e'} .'{d:e}'更改为{'d':'e'}

In [57]: data = {'A':{'B':{ 'c':{'d':'e'}}}}

In [58]: print yaml.dump(data)
A:
  B:
    c: {d: e}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM