admin管理员组文章数量:1388126
I need help with outputting a dictionary value without quotes that contains double curly braces. This yaml file will be used for ansible which supports jinja.
import yaml
data = {
'user' : '{{ val }}'
}
print(yaml.dump(data))
I need the output of this to be user: {{ val }}
without quotes. No matter what I try, the output is user: '{{ val }}'
. This output needs to be single line / no block indentation.
I need help with outputting a dictionary value without quotes that contains double curly braces. This yaml file will be used for ansible which supports jinja.
import yaml
data = {
'user' : '{{ val }}'
}
print(yaml.dump(data))
I need the output of this to be user: {{ val }}
without quotes. No matter what I try, the output is user: '{{ val }}'
. This output needs to be single line / no block indentation.
1 Answer
Reset to default -1To determine how a given scalar value is emitted pyyaml calls yaml.emitter.Emitter.choose_scalar_style
, which returns an empty string if the scalar value is to be emitted as is. Since the default dumper inherits this method, you can override it to make it always return an empty string so that even a string with special characters would be emitted without quotes:
import yaml
class PlainDumper(yaml.Dumper):
def choose_scalar_style(self):
return ''
data = {
'user' : '{{ val }}'
}
print(yaml.dump(data, Dumper=PlainDumper))
This outputs:
user: {{ val }}
本文标签: pythonPython3 YAML double curly brace no quotesStack Overflow
版权声明:本文标题:python - Python3 YAML double curly brace no quotes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744486767a2608495.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
user: {{ val }}
is not valid for YAML in the sense that given content doesn't correspond to the keyuser
mapped to the string value{{ val }}
. You may name the resulting format as "YAML which support Jinja templating", but this format is incompatible with the format named "YAML". Andyaml.dump()
outputs data exactly in YAML format. – Tsyvarev Commented Mar 19 at 13:32