简体   繁体   English

Python ruamel.yaml - 在转储 yaml 时保留单引号

[英]Python ruamel.yaml - preserving single quote in value while dumping yaml

I am trying to dump the dictionary into yaml and facing the following issue.我正在尝试将字典转储到 yaml 中并面临以下问题。

connection = dict()
connection['authentication']= 'Username Password'
connection['dbname']= '{{client}}_DB'

ruamel.yaml.round_trip_dump(connection, sys.stdout)

What i got :我得到了什么:

  connection:
    authentication: Username Password
    dbname: '{{client}}_DB'

We can see that single quote are removed from authentication but not from dbname.我们可以看到单引号已从身份验证中删除,但未从 dbname 中删除。

What i expected:我所期望的:

  connection:
    authentication: 'Username Password'
    dbname: '{{client}}_DB'

Complete example from terminal来自终端的完整示例

>>> import sys
>>> import ruamel.yaml
>>> connection = dict()
>>> connection['authentication']= 'Username Password'
>>> connection['dbname']= '{{client}}_DB'
>>> ruamel.yaml.round_trip_dump(connection, sys.stdout)
authentication: Username Password
dbname: '{{client}}_DB'
>>> print(connection)
{'authentication': 'Username Password', 'dbname': '{{client}}_DB'}
>>> 

yaml is printed without single quote. yaml 没有单引号打印。 My requirement is to have single quote around the value我的要求是在价值周围有单引号

Quotes here just mean you input string.此处的引号仅表示您输入字符串。 If you want add single quotes to 'Username Password' just use double quotes:如果您想在“用户名密码”中添加单引号,只需使用双引号:

connection['authentication'] = "'Username Password'"

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

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