Reputation: 8786
So I have a list like this:
l = ['ID', 'Format', 'Width', 'Height', '\xc2\xa0', 'ID', 'Format', 'Width', 'Height', '0', 'original', 'original', 'original', '88', 'JPG', '247', '131', '1', 'JPG', '172', '128', '90', 'JPG', '240', '320']
I am trying to make a HTML table out of this list so that it looks like this:
As you can see, every 4th element is blank (starting with 0 as the index of the first element) and after every 8th element creates a new row. I tried this:
def reformat_html_id_table(line):
htmlTable = '<table>\n'
for i in range(0, len(line)):
if (i < 0 and i % 3 == 0):
htmlTable = htmlTable + '\t<td> </td>\n'
elif i % 9 == 0:
if i == 0:
htmlTable = htmlTable + '<tr>\n'
else:
htmlTable = htmlTable + '</tr>\n<tr>\n'
htmlTable = htmlTable + '\t<td>{0}</td>\n'.format(line[i])
else:
htmlTable = htmlTable + '\t<td>{0}</td>\n'.format(line[i])
htmlTable = htmlTable + '</tr>\n</table>'
print htmlTable
This yields:
<table>
<tr>
<td>ID</td>
<td>Format</td>
<td>Width</td>
<td>Height</td>
<td> </td>
<td>ID</td>
<td>Format</td>
<td>Width</td>
<td>Height</td>
</tr>
<tr>
<td>0</td>
<td>original</td>
<td>original</td>
<td>original</td>
<td>88</td>
<td>JPG</td>
<td>247</td>
<td>131</td>
<td>1</td>
</tr>
<tr>
<td>JPG</td>
<td>172</td>
<td>128</td>
<td>90</td>
<td>JPG</td>
<td>240</td>
<td>320</td>
</tr>
</table>
Which is sortof correct, the first row is correct, but after that it doesn't and the blank data again and I am confused as to why. Can anyone show me what I did wrong? The expected output is:
<table>
<tr>
<td>ID</td>
<td>Format</td>
<td>Width</td>
<td>Height</td>
<td> </td>
<td>ID</td>
<td>Format</td>
<td>Width</td>
<td>Height</td>
</tr>
<tr>
<td>0</td>
<td>original</td>
<td>original</td>
<td>original</td>
<td> </td>
<td>88</td>
<td>JPG</td>
<td>247</td>
<td>131</td>
</tr>
<tr>
<td>1</td>
<td>JPG</td>
<td>172</td>
<td>128</td>
<td> </td>
<td>90</td>
<td>JPG</td>
<td>240</td>
<td>320</td>
</tr>
</table>
Upvotes: 0
Views: 2846
Reputation: 336
Just slightly modifying your own code, which was pretty good
def reformat_html_id_table(line):
htmlTable = '<table>\n'
mod = 9
for i in range(0, len(line)):
if (i > mod and i%mod == 5):
htmlTable = htmlTable + '\t\t<td> </td>\n'
if (i-9) % mod == 0:
if i == 0:
htmlTable = htmlTable + '\t<tr>\n'
else:
htmlTable = htmlTable + '\t</tr>\n\t<tr>\n'
htmlTable = htmlTable + '\t\t<td>{0}</td>\n'.format(line[i])
else:
htmlTable = htmlTable + '\t\t<td>{0}</td>\n'.format(line[i])
if i==9:
mod=8
htmlTable = htmlTable + '\t</tr>\n</table>'
print htmlTable
It seemed to be a problem with having the first row of 9 elements and the rest of 8
Upvotes: 1
Reputation: 645
Part of the problem is that you do have the empty column in the first entry, but not in the second and third.
I kept it simple by deleting that element first and then just adding a blank td after the third column in each row:
def reformat_html_id_table(line):
del(line[4]) # remove '\xc2\xa0'
htmlTable = '<table>\n'
for i in range(0, len(line)):
posInRow = i % 8
# begin row
if (posInRow == 0):
htmlTable += '<tr>'
htmlTable += '\t<td>{0}</td>\n'.format(line[i])
# add empty row
if (posInRow == 3):
htmlTable += '\t<td> </td>\n'
# end row
if (posInRow == 7):
htmlTable += '</tr>'
htmlTable += '</table>'
print htmlTable
Upvotes: 1
Reputation: 29094
Use this code to create HTML tables from list:
def print_table(data, row_length):
print '<table>'
counter = 0
for element in data:
if counter % row_length == 0:
print '<tr>'
print '<td>%s</td>' % element
counter += 1
if counter % row_length == 0:
print '</tr>'
if counter % row_length != 0:
for i in range(0, row_length - counter % row_length):
print '<td> </td>'
print '</tr>'
print '</table>'
Upvotes: 1