Python:for wsgi脚本中的循环

Python:for wsgi脚本中的循环,python,wsgi,Python,Wsgi,我想在wsgi脚本的html部分嵌入一个forloop my_list=[1,2,3] def my_function(environ,start_response): start_response('200 OK', [('Content-Type', 'text/html')]) return [''' <html> <body> for i in m

我想在wsgi脚本的html部分嵌入一个forloop

my_list=[1,2,3]
def my_function(environ,start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return ['''
            <html>
                 <body>
                      for i in my_list:
                       <p>
                          i
                       </p>
                <body>
            </html>
           ''']  
my_list=[1,2,3]
def my_功能(环境、启动_响应):
开始响应('200OK',[('Content-Type','text/html'))
返回['''
对于我的_列表中的i:

我

''']

如何使python代码在wsgi响应中运行?

您想做的是模板语言或库帮助我们做的事情。因为您不想使用任何模板语言/库,所以需要自己构造所需的字符串。对于此特定实例,您可以执行以下操作:

s = '''\
<html>
    <body>{list}
    </body>
</html>'''

item = '''
        <p>
            {0}
        </p>\
'''
print(s.format(list="".join(map(item.format, [1, 2, 3]))))
s=''\
{list}
'''
项目=“”

{0}

\ ''' 打印(s.format(list=”“.join)(map(item.format,[1,2,3]))
结果:

<html>
    <body>
        <p>
            1
        </p>
        <p>
            2
        </p>
        <p>
            3
        </p>
    </body>
</html>


1.

2.

3.


恭喜你!你有问题吗?快点,否则你会得到很多反对票!为什么要在字符串中执行此操作?我无法在没有模板的html中找到使用python代码的方法,这是我的问题。很抱歉,我对wsgi没有经验。如何在该html字符串中转义该for循环并使其作为python代码运行。你没有。在绳子外面做。就像在任何其他Python程序中一样。