I'm using the code below to print out rows containing 4 columns. How would I append each value in the list to a HTML table that also contains rows with four columns?
random_list = ['car', 'plane', 'van', 'boat', 'ship', 'jet','shuttle']
for i in xrange(0, len(food_list), 4):
print '\t'.join(food_list开发者_开发百科[i:i+4])
With some minor modification...
food_list = ['car', 'plane', 'van', 'boat', 'ship', 'jet','shuttle']
for i in xrange(0, len(food_list), 4):
print '<tr><td>' + '</td><td>'.join(food_list[i:i+4]) + '</td></tr>'
This basically changes the delimiter to not be tab, but the table elements. Also, puts the open row and close row at the beginning and end.
Slight variation on orangeoctopus' answer, using another join, rather than concatenation:
random_list = ['car', 'plane', 'van', 'boat', 'ship', 'jet','shuttle']
print "<table>"
for i in xrange(0, len(random_list), 4):
print ''.join(['<tr><td>','</td><td>'.join(random_list[i:i+4]),'</td></tr>'])
print '</table>'
精彩评论