2016-12-28 85 views
0

我想輸出一個我打算使用「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 
+0

您可能想嘗試轉義'%'和'{}'字符,並且您錯過了'password' – Skycc

+0

它是'%%'在格式字符串中轉義'%'。 –

+1

@Skycc,'{}'不需要在這裏轉義。 –

回答

3

您使用Python的字符串格式,其中%字符告訴它將進行替換(替換的細節由以下字符或字符確定)。您還希望在輸出中出現一些原始%字符,但%{會像您嘗試進行替換一樣查找Python,但%之後的字符不正確。

要使輸出中出現%個字符,您需要將它們加倍以避免出現這些字符。表達式"%s %% %s" % ("foo", "bar")變成"foo % bar"

相關問題