string.template with arrays as placeholders

﹥>﹥吖頭↗ 提交于 2019-12-11 22:54:06

问题


I have a string that I'm trying to format that looks like this:

output = """<div class="form-group" id="$info["id"]_container">"""

The formatting call looks like this:

Template(output).substitute(locals())

My problem is that the substitution is working, but it's substituting all of the values of the info array instead of the id value that I want.

Here's the output:

<div class="form-group" id="{'name': 'my_name', 'type': 'my_type', 'label': 'my_label', 'help_text': 'my_help_text', 'id': 'my_id', 'placeholder': 'my_placeholder', 'class': 'my_class'}["id"]_container">

Here's the output that I want:

<div class="form-group" id="my_id_container">

I've tried using {$info["id"]}, and it just treats it as part of the string and there's no substitution. I've also tried ${$info["id"]}, and it gives:

ValueError: Invalid placeholder in string: line 1, col 51

I would really like to accomplish this without using the %s syntax. Is this possible?

For reference, here's all of the code:

   info = {
        "type" : "my_type",
        "name" : "my_name",
        "id" : "my_id",
        "class" : "my_class",
        "label" : "my_label",
        "placeholder" : "my_placeholder",
        "help_text" : "my_help_text"
    }

        output = """<div class="form-group" id="$info["id"]_container">"""
        output_sub = Template(output).substitute(locals()) 

回答1:


I suggest using the new .format() string formatting syntax:

output = """<div class="form-group" id="{info_id}_container">""".format(info_id=info['id'])

or even:

info = {'id': 'my_id'}
output = """<div class="form-group" id="{id}_container">""".format(**info)



回答2:


You have a dict called info in your code so you are replacing $info with that because the value of the key info in locals() is that dict, if you want to use the values in info just use the info dict:

Use:

 print(string.Template(output).substitute(**info))
<div class="form-group" id="id["id"]_container">

Or:

output = """<div class="form-group" id="$id["id"]_container">"""
print(string.Template(output).substitute(info))



In [3]: info = {'name': 'name', 'type': 'type', 'label': 'label', 'help_text': 'help_text', 'id': 'id', 'placeholder': 'placeholder', 'class': 'class'}

In [4]: locals()["info"] # what your code is accessing
Out[4]: 
{'class': 'class',
 'help_text': 'help_text',
 'id': 'id',
 'label': 'label',
 'name': 'name',
 'placeholder': 'placeholder',
 'type': 'type'}



回答3:


As keyword in Template can't be an expression you can't use a keyword like info["id"] : if you want to use Template you need an output like :

output = """<div class="form-group" id="${info}_container">"""

But in other way you can use format or %s .

>>> output = """<div class="form-group" id="${info}_container">"""
>>> print string.Template(output).substitute(info=info["id"])
<div class="form-group" id="id_container">


来源:https://stackoverflow.com/questions/26950301/string-template-with-arrays-as-placeholders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!