输入: -
CRlist
[['CR', 'FA', 'CL', 'TITLE'], ['409452', 'WLAN', '656885', 'Age out RSSI values from buffer in Beacon miss scenario'], ['379104', 'BT', '656928', 'CR379104: BT doesn\xe2\x80\x99t work that Riva neither sends HCI Evt for HID ACL data nor response to HCI_INQUIRY after entering into pseudo sniff subrating mode.']]
我有以下pythong代码生成HTML代码,但它生成的输出是不期望的,我pring数组值之前似乎有正确的数据在这里,但在使用.format的东西是得到messedup..can任何人点这有什么不对?
for i in range(len(CRlist)):
if i==0:
continue
for j in range(len(CRlist[0])):
print "i"
print i
print "j"
print j
print "CRlist[i][j]"
print CRlist[i][j]//right data here
CRstring += """
<tr>
<td><a href="{CR}">{CR}</a></td>
<td>{FA}</td>
<td>{CL}</td>
<td>{Title}</td>
</tr>""".format(
CR=CRlist[i][j],
FA=CRlist[i][j],
CL=CRlist[i][j],
Title=CRlist[i][j],
)
CRstring += "\n</table>\n"
我对输出的期望却被错误地创建了
<tr>
<td><a href="409452">409452</a></td>
<td>WLAN</td>
<td>656885</td>
<td>Age out RSSI values from buffer in Beacon miss scenario</td>
</tr>
..............
实际输出,因为行单元格数据是多余的
<tr>
<td><a href="409452">409452</a></td>
<td>409452</td>
<td>409452</td>
<td>409452</td>
</tr>
<tr>
<td><a href="WLAN">WLAN</a></td>
<td>WLAN</td>
<td>WLAN</td>
<td>WLAN</td>
</tr>
<tr>
<td><a href="656885">656885</a></td>
<td>656885</td>
<td>656885</td>
<td>656885</td>
</tr>
<tr>
<td><a href="Age out RSSI values from buffer in Beacon miss scenario">Age out RSSI values from buffer in Beacon miss scenario</a></td>
<td>Age out RSSI values from buffer in Beacon miss scenario</td>
<td>Age out RSSI values from buffer in Beacon miss scenario</td>
<td>Age out RSSI values from buffer in Beacon miss scenario</td>
</tr>
<tr>
<td><a href="379104">379104</a></td>
<td>379104</td>
<td>379104</td>
<td>379104</td>
</tr>
<tr>
<td><a href="BT">BT</a></td>
<td>BT</td>
<td>BT</td>
<td>BT</td>
</tr>
<tr>
<td><a href="656928">656928</a></td>
<td>656928</td>
<td>656928</td>
<td>656928</td>
</tr>
<tr>
<td><a href="CR379104: BT doesnΓÇÖt work that Riva neither sends HCI Evt for HID ACL data nor response to HCI_INQUIRY after entering into pseudo sniff subrating mode.">CR379104: BT doesnΓÇÖt work that Riva neither sends HCI Evt for HID ACL data nor response to HCI_INQUIRY after entering into pseudo sniff subrating mode.</a></td>
<td>CR379104: BT doesnΓÇÖt work that Riva neither sends HCI Evt for HID ACL data nor response to HCI_INQUIRY after entering into pseudo sniff subrating mode.</td>
<td>CR379104: BT doesnΓÇÖt work that Riva neither sends HCI Evt for HID ACL data nor response to HCI_INQUIRY after entering into pseudo sniff subrating mode.</td>
<td>CR379104: BT doesnΓÇÖt work that Riva neither sends HCI Evt for HID ACL data nor response to HCI_INQUIRY after entering into pseudo sniff subrating mode.</td>
</tr>
/table>
====== PLlist ==========
答案 0 :(得分:0)
此代码为每个模板变量提供相同的值:
CR=CRlist[i][j],
FA=CRlist[i][j],
CL=CRlist[i][j],
Title=CRlist[i][j],
显然,这不符合你的意图。这是另一种写作方式:
TEMPLATE = """
<tr>
<td><a href="{CR}">{CR}</a></td>
<td>{FA}</td>
<td>{CL}</td>
<td>{Title}</td>
</tr>
"""
for i, item in enumerate(CRlist):
if i == 0:
continue
CRstring += TEMPLATE.format(
CR=item[0],
FA=item[1],
CL=item[2],
Title=item[3],
)
CRstring += "\n</table>\n"
您甚至可以通过切片列表删除i
和enumerate
位:
for item in CRList[1:]:
CRstring += # ...
由于您正在生成HTML并且正在使用用户输入(我假设,至少)并且没有转义HTML,因此您有一个XSS漏洞。让我们解决这个问题:
# near the top of the file:
import cgi
# later...
# ...
CRstring += TEMPLATE.format(
CR=cgi.escape(item[0]),
FA=cgi.escape(item[1]),
# ...
)
这一切都很好,但正如有人在评论中指出的那样,你可能最好使用真正的模板引擎。我个人喜欢Jinja2。这是你如何做到的:
{%- for item in cr_list[1:] %}
<tr>
<td><a href="{{ item[0] | escape }}">{{ item[0] | escape }}</a></td>
<td>{{ item[1] | escape }}</td>
<td>{{ item[2] | escape }}</td>
<td>{{ item[3] | escape }}</td>
</tr>
{%- endfor %}
</table>
此外,您可能希望将数据放入对象中。例如:
class CREntry(object):
def __init__(self, cr, fa, cl, title):
self.cr = cr
self.fa = fa
self.cl = cl
self.title = title
然后你可以很简单地转换它:
entries = [CREntry(*entry) for entry in CRlist[1:]]
然后您的代码变得更加清晰,能够引用entry.title
而不是item[3]
。
您可能还想使用PEP 8中列出的常规Python约定。
如果你已完成,你的代码如下:
import jinja2
env = jinja2.Environment(autoescape=True) # no more | escape everywhere!
template = env.from_string(r"""
{%- for entry in entries %}
<tr>
<td><a href="{{ entry.cr }}">{{ entry.cr }}</a></td>
<td>{{ entry.fa }}</td>
<td>{{ entry.cl }}</td>
<td>{{ entry.title }}</td>
</tr>
{%- endfor %}
</table>
""")
class CREntry(object):
# ...
# later...
entries = [CREntry(*entry) for entry in cr_list]
cr_string = template.render(entries=entries)
在其他地方添加更多代码,但在实际生成HTML时更少,而且我会说 更易于维护。