遍历CGI文件中的列表并在html中输出值 [英] Iterate through a list in a CGI file and print out values in html
问题描述
我在cgi文件中有两个列表:
Numbers = [0,1,2,3]
字母= [A,B,C,D]
如何迭代这些列表并在html中输出值
理想情况下,我的表格如下所示:
0 A
1 B
<2>
<3>
等等。
这意味着表格和行将不得不由我的列表多长时间以及我拥有多少
列表来决定。因此,我还需要知道如何遍历列表并在html脚本中创建
表格,并在列表中进行迭代。
我已经完成了到目前为止:
print'''
< html>
< head>
< / head>
< body>
< center>
< table border =0cellspacing =15>
< tr>
< td align =centerstyle =font-size:1.25em;>
< p class =sansserif> < b取代;号码:%d< / b> <峰; br>
字母:%s< / p>
< / td>
< / tr>
< / table>
< / center>
< / body>
< / html>'''%(数字,字母)
真的没有遍历列表,我只知道列表大小,并已完成
所需的代码。此外,这只是打印出来:
0
A
在表格的单元格内
有两种选择:
使用标准字符串格式化函数
您尝试通过%
创建结果内容就是朝这个方向发展。 / p>
但是,由于循环(输出中的行),并且既不是%
也不是 string.format
支持循环,您必须在您的代码中创建此循环内容,并最终嵌入到结果页面中。
bigtempl ='''< html>
< head>
< / head>
< body>
< center>
< table border =0cellspacing =15>
{rows}
< / table>
< / center>
< / body>
< / html>'''
rowtempl =
< tr>
< td align =centerstyle =font-大小:1.25em;>
< p class =sansserif>< b>编号:{编号:d}< / b>< br>
字母:{字母}< / p>
< / td>
< / tr>
数字= [0,1,2,3]
letters = [A,B,C,D]
lst = zip(数字,字母)
rows = [ rowtempl.format(number = number,letter = letter)for number,in lst]
rows =.join(rows)
wholepage = bigtempl.format(rows = rows)
打印整页
使用高级模板库
有许多软件包,允许基于模板和数据结构生成内容。这些通常允许循环。
我曾经决定继续使用 jinja2
,我对此很满意。 Jinja2中的任务如下所示:
import jinja2
templ ='''< html> ;
< head>
< / head>
< body>
< center>
< table border =0cellspacing =15>
{数字%,lst%中的字母}
< tr>
< td align =centerstyle =font-size:1.25em;>
< p class =sansserif> < b取代;号码:{{number}}< / b> <峰; br>
字母:{{letter}}< / p>
< / td>
< / tr>
{%endfor%}
< / table>
< / center>
< / body>
< / html>'''
数字= [0,1,2,3]
letters = [A,B,C, D]
lst = zip(数字,字母)
template = jinja2.Template(templ)
print template.render(lst = lst)
其他模板解决方案以非常相似的方式来完成。
I have two list in a cgi file:
Numbers = [0, 1, 2, 3]
Letters = [A, B, C, D]
How do I iterate through these list and print the values out in html
I ideally my table would look like this:
0 A
1 B
2 C
3 D
and etc.
That means the tables and rows will have to be dictated by how long my list is and how many list I have. Thus I also would need to know how to iterate through the list and create the table in html script as I iterate through the list.
I have done this so far:
print'''
<html>
<head>
</head>
<body>
<center>
<table border="0" cellspacing="15">
<tr>
<td align="center" style="font-size:1.25em;">
<p class="sansserif"> <b> Number: %d </b> <br>
Letter: %s </p>
</td>
</tr>
</table>
</center>
</body>
</html>'''%(Number, Letter)
But this is really not iterating through the list, I just know the list size and have done the necessary code for it. Also this just prints outs:
0
A
within a cell of a table
There are two options
Using standard string formatting functions
Your attempt to create resulting content by %
is going this direction.
However, as there are loops (rows in your output), and as neither %
nor string.format
support looping, you would have to create this "looping content" in your code and finally embed in resulting page.
bigtempl = '''<html>
<head>
</head>
<body>
<center>
<table border="0" cellspacing="15">
{rows}
</table>
</center>
</body>
</html>'''
rowtempl = """
<tr>
<td align="center" style="font-size:1.25em;">
<p class="sansserif"> <b> Number: {number:d} </b> <br>
Letter: {letter} </p>
</td>
</tr>
"""
numbers = [0, 1, 2, 3]
letters = ["A", "B", "C", "D"]
lst = zip(numbers, letters)
rows = [rowtempl.format(number=number, letter=letter) for number, letter in lst]
rows = "".join(rows)
wholepage = bigtempl.format(rows=rows)
print wholepage
Using advanced template library
There are many packages, allowing to generate content based on templates and data structures. These often allow looping.
I once decided to keep using jinja2
and I am happy with that. Your task looks like this in Jinja2:
import jinja2
templ = '''<html>
<head>
</head>
<body>
<center>
<table border="0" cellspacing="15">
{% for number, letter in lst %}
<tr>
<td align="center" style="font-size:1.25em;">
<p class="sansserif"> <b> Number: {{number}} </b> <br>
Letter: {{letter}} </p>
</td>
</tr>
{% endfor %}
</table>
</center>
</body>
</html>'''
numbers = [0, 1, 2, 3]
letters = ["A", "B", "C", "D"]
lst = zip(numbers, letters)
template = jinja2.Template(templ)
print template.render(lst=lst)
Other templating solutions do that in very similar fashion.
这篇关于遍历CGI文件中的列表并在html中输出值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!