hsel hsel -4 years ago 168
Python Question

Python and Html - Producing Tables

I was wondering how you would get rid of the 'stray' box (circled in blue) that would be made from a 2 column table. This is what happens:Table

Here is my python code:

file_name = 'test.html'

test_file = open('test.html', 'w')

test_file.write('''<!DOCTYPE html>
<html>
<head>
<title> Dogs </title>
</head>
<body>
<table width="500" border="2" cellpadding="5">
''')

dog_names = ['Bob', 'Kenny', 'Robin', 'Ben', 'Tom', 'Steve']
cat_names = ['Gob', 'Smith', 'Tod', 'Flamingo', 'Dragon', 'Fluffy']

index_dog = 0
index_cat = 0
amount = 5

for i in range(amount):
if index_dog % 2 == 0:
test_file.write('''
<tr>
<td align="center" valign="center">
<p>''' + dog_names[index_dog]+ '''</p>
</td>''')
else:
test_file.write(''' <td align="center" align="center">
<p>''' + dog_names[index_dog]+ '''</p>
</td>
</tr>\n''')
index_dog += 1

for i in range(amount):
if index_cat % 2 == 0:
test_file.write('''
<tr>
<td align="center" valign="center">
<p>''' + cat_names[index_cat]+ '''</p>
</td>''')
else:
test_file.write(''' <td align="center" align="center">
<p>''' + cat_names[index_cat]+ '''</p>
</td>
</tr>\n''')
index_cat += 1

test_file.write('''\n </table>
</body>
</html> ''')


HTML code produced by Python:

<!DOCTYPE html>
<html>
<head>
<title> Dogs </title>
</head>
<body>
<table width="500" border="2" cellpadding="5">

<tr>
<td align="center" valign="center">
<p>Bob</p>
</td> <td align="center" align="center">
<p>Kenny</p>
</td>
</tr>

<tr>
<td align="center" valign="center">
<p>Robin</p>
</td> <td align="center" align="center">
<p>Ben</p>
</td>
</tr>

<tr>
<td align="center" valign="center">
<p>Tom</p>
</td>
<tr>
<td align="center" valign="center">
<p>Gob</p>
</td> <td align="center" align="center">
<p>Smith</p>
</td>
</tr>

<tr>
<td align="center" valign="center">
<p>Tod</p>
</td> <td align="center" align="center">
<p>Flamingo</p>
</td>
</tr>

<tr>
<td align="center" valign="center">
<p>Dragon</p>
</td>
</table>
</body>
</html>


Thank you in advanced.

Answer Source

You need to keep a shared index between dogs and cats, to remember if you finish dogs on the left or the right

shared_index = 0
for i in range(amount):
    if shared_index % 2 == 0:
        test_file.write('''          
        <tr>
            <td align="center" valign="center">
                <p>''' + dog_names[i]+ '''</p>
            </td>''')
    else:
        test_file.write('''    <td align="center" align="center">
                <p>''' + dog_names[i]+ '''</p>
            </td>
        </tr>\n''')
    shared_index += 1

for i in range(amount):
    if shared_index % 2 == 0:
        test_file.write('''          
        <tr>
            <td align="center" valign="center">
                <p>''' + cat_names[i]+ '''</p>
            </td>''')
    else:
        test_file.write('''    <td align="center" align="center">
                <p>''' + cat_names[i]+ '''</p>
            </td>
        </tr>\n''')
    shared_index += 1

But you have six dog names and cat names. By limiting to 5, you will never see Steve or Fluffy

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download