0

I'm stuck on creating a HTML Table for this code here. have to try to output all the dir() of list, dict, tuple, str, int, and float in a HTML table with a limit of 7 column per row. I've tried many things and is unable to produce that. I kept getting an internal server error when i try it out. I think i'm doing the tags from for the table or i'm missing something but still haven't figured it out.

#!/usr/local/bin/python3

print('Content-type: text/html\n')

d_list = [] 
d_dict = []
d_tuple = []
d_str = []
d_int = []
d_float = []

for i in dir(list):
    d_list.append(i)
for i in dir(dict):
    d_dict.append(i)
for i in dir(tuple):
    d_tuple.append(i)
for i in dir(str):    
    d_str.append(i)
for i in dir(int):
    d_int.append(i)
for i in dir(float):
    d_float.append(i)

d_list = [d_list[i:i + 7] for i in range(0, len(d_list), 7)]
d_dict = [d_dict[i:i + 7] for i in range(0, len(d_dict), 7)]
d_tuple = [d_tuple[i:i + 7] for i in range(0, len(d_tuple), 7)]
d_str = [d_str[i:i + 7] for i in range(0, len(d_str), 7)]
d_int = [d_int[i:i + 7] for i in range(0, len(d_int), 7)]
d_float = [d_float[i:i + 7] for i in range(0, len(d_float), 7)]

td_list = []
td_dict = []
td_tuple = []
td_str = []
td_int = []
td_float = []

for i in d_list:
     td_list.append(['<td>{}</td>'.format(x) for x in i])
for i in d_dict:
     td_dict.append(['<td>{}</td>'.format(x) for x in i])
for i in d_tuple:
     td_tuple.append(['<td>{}</td>'.format(x) for x in i])
for i in d_str:
     td_str.append(['<td>{}</td>'.format(x) for x in i])
for i in d_int:
     td_int.append(['<td>{}</td>'.format(x) for x in i])
for i in d_float:
     td_float.append(['<td>{}</td>'.format(x) for x in i])

<table>
for i in td_list:
     print('<tr>' + " ".join(i) + '</tr>')
</table>
<table>
for i in td_dict:
     print('<tr>' + " ".join(i) + '</tr>')
</table>
<table>
for i in td_tuple:
     print('<tr>' + " ".join(i) + '</tr>')
</table>
<table>
for i in td_str:
     print('<tr>' + " ".join(i) + '</tr>')
</table>
<table>
for i in td_int:
     print('<tr>' + " ".join(i) + '</tr>')
</table>
<table>
for i in td_float:
     print('<tr>' + " ".join(i) + '</tr>')
</table>
2
  • If you're printing the HTML, shouldn't you also be printing the table tags? Commented Jul 7, 2014 at 21:50
  • Can you post some sample output? Also, @CDspace has an excellent point. Commented Jul 7, 2014 at 21:52

1 Answer 1

1

Try this

#!/usr/local/bin/python3

print('Content-type: text/html\n')

table = []
table.append("<table>\n")
for obj in [list, dict, tuple, str, int, float]:
    table.append("\t<tr>\n")
    td = []
    for key in dir(obj)[:7]:
        td.append("<td>{0}</td>".format(key))
    table.append("\t\t"+"".join(td))
    table.append("\n\t</tr>\n")

table.append("</table>")

print("".join(table))
Sign up to request clarification or add additional context in comments.

1 Comment

when i run it, it just outputs the first 7 of the dir and then stops and goes to the next class. so i tried putting the object for loop above table = [] so itll create a table for each class and i took out the [:7] thing on the second for loop. this works but now the table is just one long row per table. is there any way to limit it back to 7 but also output all the dir from each class.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.