不受支持的格式字符' {'

时间:2016-12-28 03:06:51

标签: python

我想输出一个我想与" hiera"一起使用的yaml文件。我想输出看起来像这样的东西:

---
top: '/tmp'
package: 'mypackage'
version: 1
password: 'cubswin:)'
make_audiobook::package_name: "%{hiera('package')}"
make_audiobook::version: "%{hiera('version')}"
make_audiobook::mp3filesdir: "%{hiera('top')}/%{::appname}/%{::appname}/res/raw"
make_audiobook::app_image::app_image_url: 'http://big/long/path'
make_audiobook::app_image::resource_path:  "%{hiera('top')}/%{::appname}/%{::appname}"
make_audiobook::app_javacode::appname:          "%{::appname}"
make_audiobook::app_javacode::password:         "%{hiera('password')}"
make_audiobook::app_javacode::apptitle:         "The Title of The App"
make_audiobook::app_javacode::package:          "%{hiera('package')}"
make_audiobook::app_javacode::path_to_code:     "%{hiera('top')}/%{::appname}/%{::appname}/src/com/%{hiera('package')}/%{::appname}"
make_audiobook::app_javacode::path_to_androidmanifest:     "%{hiera('top')}/%{::appname}/%{::appname}"
make_audiobook::app_javacode::version:          "%{hiera('version')}"

所以我试过了......

print """
---
top: '%s'
package: '%s'
version: %s
password: '%s'
make_audiobook::package_name: "%{hiera('package')}"
make_audiobook::version: "%{hiera('version')}"
make_audiobook::mp3filesdir: "%{hiera('top')}/%{::appname}/%{::appname}/res/raw"
make_audiobook::app_image::app_image_url: '%s'
make_audiobook::app_image::resource_path:  "%{hiera('top')}/%{::appname}/%{::appname}"
make_audiobook::app_javacode::appname:          "%{::appname}"
make_audiobook::app_javacode::password:         "%{hiera('password')}"
make_audiobook::app_javacode::apptitle:         "%s"
make_audiobook::app_javacode::package:          "%{hiera('package')}"
make_audiobook::app_javacode::path_to_code:     "%{hiera('top')}/%{::appname}/%{::appname}/src/com/%{hiera('package')}/%{::appname}"
make_audiobook::app_javacode::path_to_androidmanifest:     "%{hiera('top')}/%{::appname}/%{::appname}"
make_audiobook::app_javacode::version:          "%{hiera('version')}"
make_audiobook::app_javacode::tracks:
""" % (top, package, version, app_image_url, apptitle)

但是我收到了这个错误:

Traceback (most recent call last):
  File "./get_track_titles.py", line 54, in <module>
    """ % (top, package, version, app_image_url, apptitle)
ValueError: unsupported format character '{' (0x7b) at index 88

1 个答案:

答案 0 :(得分:3)

您正在使用Python的字符串格式,其中%字符告诉它要进行替换(替换的详细信息由以下字符确定)。您还希望在输出中显示一些原始%字符,但%{看起来像您尝试进行替换,但在%之后使用不合适的字符

要使%个字符出现在输出中,您需要通过加倍来转义它们。表达式"%s %% %s" % ("foo", "bar")变为"foo % bar"