Python and HTML table

I use the following code to print rows containing 4 columns. How to add each value to a list in an 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])
+3
source share
2 answers

With some minor changes ...

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>'

Basically, this changes the delimiter so as not to be a tab, but table elements. It also places the open line and the closing line at the beginning and end.

+3
source

Slight variation on orangeoctopus response using a different 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>'
+1
source

Source: https://habr.com/ru/post/1753923/


All Articles