1

I have the following code that generates the desired HTML. But it prints it at the top of the page, instead of where I want it.

def fixText(self,text):
    row = []
    z = text.find(',')

    if z == 0: row.append('')
    else:      row.append(text[:z])

    for x in range(len(text)):
        if text[x] != ',': pass
        else:
            if x == (len(text)-1): row.append('')
            else:
                if ',' in text[(x+1):]:
                    y = text.find(',', (x+1))
                    c = text[(x+1):y]
                else:
                    c = text[(x+1):]
                    row.append(c)
    return row

def output(self):
    output = ""
    fob=open('files/contacts.txt','r')
    tup = []

    while 1:
        text = fob.readline()

        if text == "": break
        else: pass

        if text[-1] == '\n':
            text = text[:-1]
        else: pass

        row = self.fixText(text)
        tup.append(row)

    output = tup.sort(key=lambda x: x[0].lower())

    _list1 = len(tup)
    i = 0
    table = ""

    while i < _list1:
        j = 0 #need to reset j on each iteration of i
        _list2 = len(tup[i])
        print("<tr>")

        while j < _list2:
            print("<td>" + tup[i][j] + "</td>")
            j += 1

        print("</tr>")
        i += 1

    return output

As you can see, I have a nested loop that prints out the HTML code. This works fine, but when I try to inject it into my modular site it prints it at the top of the page. My hunch is that the following code is the part I need to change.

#!/usr/local/bin/python3

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

import os, sys
import cgitb; cgitb.enable()

sys.path.append("classes")

from Pagedata import Pagedata
Page = Pagedata()

print(Page.doctype())
print(Page.head())
print(Page.title("View Contact"))
print(Page.css())

html='''</head>
<body>
<div id="wrapper">
    <div id="header">
        <div id="textcontainer">{1}</div>
    </div>
    <div id="nav">{2}</div>
    <div id="content">
        <h2>Heading</h2>
        <h3>Sub Heading Page 2</h3>
        <table>
            <tbody>
                {0}
            </tbody>
        </table>    
    </div>
<div>
<div id="footer">{3}</div>
</body>
</html>'''.format(Page.output(),Page.header(),Page.nav(),Page.footer())

print(html)

There are other functions on my class page, such as header, footer, etc., that work fine.

For example, the footer is properly inserted into div#footer. But the generated table si not inserted where {0} is.

You can view the broken code here.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
AlecPerkey
  • 659
  • 2
  • 11
  • 21

2 Answers2

0

You shouldn't use print inside def output(self): because that it s printing directly during the look on sys.stdout

table = ""
while i < _list1:
    j = 0#need to reset j on each iteration of i
    _list2 = len(tup[i])
    table += "<tr>"
    while j < _list2:
        table=+"<td>" + tup[i][j] + "</td>"
        j += 1
    table += "</tr>"
    i += 1
return table

You html variable should be populated properly before you print without any print on stdout

Rachid
  • 2,463
  • 1
  • 21
  • 9
  • Sorry but you can use format with any order inside the number : `"{0} {1}".format('Hello','world') == "{1} {0}".format('world','Hello')` – Rachid Apr 15 '12 at 20:23
  • Can you show a list so it is a full example to generate a html table? – Timo Dec 24 '20 at 17:09
0

Nested loops are not optimal as compared to the built-in, preferred method of joining lists i.e using .join(), the following code is optimal in most cases and is also the more "pythonic" way

table = ""
while i < _list1:
    table += "<tr><td>{0}</td></tr>".format("</td><td>".join(tup[i]))
    i += 1
return table
Community
  • 1
  • 1
Yogesh Mangaj
  • 3,200
  • 6
  • 32
  • 45