Python YAML hiera以及如何将值放在双引号内?

时间:2017-03-24 03:38:39

标签: python yaml hiera

我有这段代码

David

它输出如下所示的yaml:

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' );

很难看到,但问题的特定键/值对是这个:

{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}

我需要这样做:

'make_audiobook::sftp_script::appname': '"%{::appname}"',

'make_audiobook::sftp_script::appname': "%{::appname}", 周围加上双引号我无法弄清楚我的python代码中要做什么。请帮助:)

2 个答案:

答案 0 :(得分:2)

如果你想在YAML中使用一个字符串,那么你只需要一个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没有区别。这样:

my_key: 'my value'

而且:

my_key: "my value"

两者都会评估完全相同的数据结构。

答案 1 :(得分:2)

如果您需要控制Python YAML转储中的引号,我建议使用ruamel.yaml(免责声明:我是该软件包的作者)

import sys
from ruamel import yaml

amazon_app_id = 'junk'
librivox_rest_url = 'https://librivox.org/api/feed/audiobooks/id/1000/extended/1/format/json'

python_data_struct = {
        'amazon_app_id': amazon_app_id,
        'librivox_rest_url': librivox_rest_url,
        'pacakge': 'junk',
        'version': 'junk',
        yaml.scalarstring.SingleQuotedScalarString('make_audiobook::sftp_script::appname'):
            yaml.scalarstring.DoubleQuotedScalarString('%{::appname}')
}

try:
    with open(hiera_dir + '/' + appname + '.yaml') as fp:
        yaml.round_trip_dump(python_data_struct, fp)
except Exception as e:
    print("Error on {} Error [{}]".format(librivox_rest_url, e))
    sys.exit(5)

给你:

librivox_rest_url: https://librivox.org/api/feed/audiobooks/id/1000/extended/1/format/json
version: junk
'make_audiobook::sftp_script::appname': "%{::appname}"
amazon_app_id: junk
pacakge: junk

请注意,PyYAML默认情况下会在嵌入了':'的标量周围加上引号,即使该字符后面没有空格,尽管这不是必需的。

同样不能将第二个参数用于round_trip_dump(),(dump()也有此参数)。不提供该参数并首先在内存中建立YAML文件是非常低效的。

我也更改了一些其他代码,除非你被困在Python 2.6左右,你应该IMO现代化你的语法。您可能还想查看键名(您的意思是'pacakge'吗?)