简体   繁体   中英

unsupported format character '{'

I want to output a yaml file I intend to use with "hiera". I want to output something that looks like this:

---
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')}"

So I tried this ...

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)

But I get this error:

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

You're using Python's string formatting, where the % character tells it that a substitution is to be made (with the details of the substitution determined by the following character or characters). You also want some raw % characters to appear in the output, but %{ looks to Python like you're trying to make a substitution, but with an inappropriate character after the % .

To make the % characters appear in the output, you need to escape them by doubling them. The expression "%s %% %s" % ("foo", "bar") becomes "foo % bar" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM