How to print a value with double quotes and spaces in YAML?

廉价感情. 提交于 2019-12-24 04:31:45

问题


I am trying to dump a Python dictionary to YAML which has some strings as value fields.

import yaml
str1 = "hello"
str2 = "world"
mystr = "\"" + str1 + str(" ") + str2 + "\""
mydict = {"a" : mystr}
f = open("temp.yaml", "w")
yaml.dump(mydict, f, default_flow_style = False, \
                    explicit_start = "---", explicit_end = "...", encoding = 'UTF-8')
f.close()

the YAML I get is:

a: '"hello
 world"'

Notice, the value "hello world" is spilling to the next line. I am using python 3.5 and YAML module version is 3.11

Can someone tell me how to make the YAML look like below?

a: "hello world"

回答1:


The code is a bit sloppy but it will give the results you want.

global dict_keys

def mk_double_quote(dumper, data):
    if data in dict_keys:
        return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='')
    else:
        return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='"')

yaml.add_representer(str, mk_double_quote)

d = {'a': 'Hello World'}
dict_keys = set(d.keys())

f = open('temp.yaml', 'w')
yaml.dump(d, f, default_flow_style=False, encoding='utf-8')
f.close()

The result will look like: a: "Hello World"




回答2:


If you want fine grain control over which string gets double (or single) quotes, you should use ruamel.yaml (disclaimer: I am the author of that package). It is an improved version of PyYAML, with many longstanding issues in PyYAML fixed.

With it you can do:

import sys
import ruamel.yaml

mystr = ruamel.yaml.scalarstring.DoubleQuotedScalarString('hello world')
mydict = dict(a=mystr)

yaml = ruamel.yaml.YAML()
yaml.dump(mydict, sys.stdout)

to get:

a: "hello world"

If you start out with a YAML document, things are even easier, you can just indicate you want to preserve these superfluous quotes:

yaml_str = """\
a: "hello world"
"""

yaml = ruamel.yaml.YAML()
yaml.preserve_quotes = True
data = yaml.load(yaml_str)
yaml.dump(data, sys.stdout)


来源:https://stackoverflow.com/questions/47542343/how-to-print-a-value-with-double-quotes-and-spaces-in-yaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!