Input:-
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.']]
I have the following pythong code to generate HTML code but its generating an output which am not expecting,I pring the array values before which seem to have right data here but while using.format something is getting messedup..can anyone point what is wrong here?
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"
My expectation of output but is getting created incorrectly
<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>
..............
Actual output,as you can the row cell data is redundant
<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==========
This code supplies the same value to each template variable:
Obviously, that does not work as you intended. Here’s another way of writing it:
You could even remove the
iandenumeratebits by slicing the list:Since you’re generating HTML and are using user input (I assume, at least) and aren’t escaping the HTML, you’ve got an XSS vulnerability. Let’s fix that, too:
Further improvements
That’s all well and fine, but as someone pointed out in the comments, you may be better off using a real template engine. I like Jinja2, personally. Here’s how you’d do that:
Furthermore, you may want to put your data into objects. For example:
Then you can convert it quite simply:
Then your code becomes much more clear, being able to reference
entry.titlerather thanitem[3].You may also want to use the normal Python conventions as outlined in PEP 8.
If you’ve got that done, your code looks like this:
A little more code elsewhere, but less when you’re actually generating the HTML, and I’d say it’s much more maintainable.