Python YAML层次结构以及双引号内的值如何? [英] Python YAML hiera and how have value be inside double quotes?
问题描述
我有此代码
python_data_struct = {
'amazon_app_id': amazon_app_id,
'librivox_rest_url': librivox_rest_url,
'librivox_id': librivox_id,
'top': top,
'pacakge': 'junk',
'version': 'junk',
'password': password,
'description': description,
'long_description': long_description,
'make_audiobook::sftp_script::appname': '\"%{::appname}\"'
}
try:
myyaml = yaml.dump(python_data_struct)
except:
e = sys.exc_info()[0]
print "Error on %s Error [%s]" % ( librivox_rest_url, e )
sys.exit(5)
write_file( myyaml, hiera_dir + '/' + appname + '.yaml' );
它输出如下所示的Yaml:
It outputs yaml that looks like this:
{amazon_app_id: junk, description: !!python/unicode ' Riley was an American writer
known as the "Hoosier poet", and made a start writing newspaper verse in Hoosier
dialect for the Indianapolis Journal in 1875. His favorite authors were Burns
and Dickens. This collection of poems is a romanticized and mostly boy-centered
paean to a 19th century rural American working-class childhood. (Summary by Val
Grimm)', librivox_id: '1000', librivox_rest_url: 'https://librivox.org/api/feed/audiobooks/id/1000/extended/1/format/json',
long_description: !!python/unicode "\n Riley was an American writer known as the\
\ \"Hoosier poet\", and made a start writing newspaper verse in Hoosier dialect\
\ for the Indianapolis Journal in 1875. His favorite authors were Burns and Dickens.\
\ This collection of poems is a romanticized and mostly boy-centered paean to\
\ a 19th century rural American working-class childhood. (Summary by Val Grimm)\n\
\nThe \"Selected Riley Child-Rhymes\" App will not use up your data plan's minutes\
\ by downloading the audio files (mp3) over and over. You will download load all\
\ the data when you install the App. The \"Selected Riley Child-Rhymes\" App works\
\ even in airplane mode and in places without Wi-Fi or phone network access! So\
\ you can listen to your audio book anywhere anytime! The \"Selected Riley Child-Rhymes\"\
\ App automatically pauses when you make or receive a phone call and automatically\
\ resumes after the call ends. The \"Selected Riley Child-Rhymes\" App will continue\
\ to play until you exit the App or pause the reading so it is perfect for listening\
\ in bed, at the gym, or while driving into work.\" \n", 'make_audiobook::sftp_script::appname': '"%{::appname}"',
pacakge: junk, password: junk, top: junk, version: junk}
很难看到,但是有问题的特定键/值对就是这个:
It is hard to see but the particular key/value pair that is a problem is this one:
'make_audiobook::sftp_script::appname': '"%{::appname}"',
我需要这样:
'make_audiobook::sftp_script::appname': "%{::appname}",
在%{::appname}
周围仅用双引号引起来,我无法弄清楚在python代码中该做什么.请帮忙:)
Just double quotes around %{::appname}
I cannot figure out what to do in my python code. Please help :)
推荐答案
如果您要使用YAML中的字符串,则只需要使用Python中的字符串即可:
If you want a string in YAML, then you just need a string in Python:
python_data_struct = {
'make_audiobook::sftp_script::appname': '%{::appname}'
}
print yaml.dump(python_data_struct)
哪个给您:
{'make_audiobook::sftp_script::appname': '%{::appname}'}
或等效地,使用default_flow_style=False
:
make_audiobook::sftp_script::appname: '%{::appname}'
使用双引号的"a value"
和使用单引号的'a value'
之间在YAML中没有什么不同.这个:
There is no different in YAML between "a value"
using double quotes and 'a value'
using single quotes. This:
my_key: 'my value'
这:
my_key: "my value"
两者都将得出完全相同的数据结构.
Will both evaluate to the exact same data structure.
这篇关于Python YAML层次结构以及双引号内的值如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!