Hello
I try to get py output (s_subject) to a web bage table using html code. Thing is that result is in one line. I'd like to have it for each subject one line. Pls can you show me how to make it?
Example:

Predmet: 
         maths
         fyzics
         history

here is a code:

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines



    import webbrowser
    firefox = webbrowser.get()

    subjects=['maths','fyzics','history']
    space=' '

    def stud_subject():
        s=''
        for i in subjects:
            s+=i+'\n'
        s_subject=s
        return s_subject




    def writing():
        vytvor=open('vysvedcenie_test.html','w')

        line1='<table border="1" cellpadding="15" cellspacing="5" width="100%">'
        line2='<tr>'
        line3='<th colspan="2">Vysvedcenie</th>'
        line4='</tr>'
        line5='<tr>'
        line6='<td width="50%">Meno, Priezvisko:</td>\<td>Skolsky rok:</td>'
        line7='</tr>'
        line8='<tr>'
        line9='<td width="25%">Kruzok:</td>'
        line10='</tr>'
        line11='<tr>'
        line12='<td width="50%">Predmety:'+space+stud_subject()+'</td>\ <td>Hodnotenie:</td>'
        line13='</tr>'
        line14='<tr>'
        line15='<td width="50%">Celkovy prospech:</td>'
        line16='</tr>'
        line17='<tr>'
        line18='<td width="50%">Menovany student presiel do dalsieho rocnika (ano/nie):</td>'
        line19='</tr>'
        line20='</table>'


        vytvor.write(line1)
        vytvor.write(line2)
        vytvor.write(line3)
        vytvor.write(line4)
        vytvor.write(line5)
        vytvor.write(line6)
        vytvor.write(line7)
        vytvor.write(line8)
        vytvor.write(line9)
        vytvor.write(line10)
        vytvor.write(line11)
        vytvor.write(line12)
        vytvor.write(line13)
        vytvor.write(line14)
        vytvor.write(line15)
        vytvor.write(line16)
        vytvor.write(line17)
        vytvor.write(line18)
        vytvor.write(line19)
        vytvor.write(line20)
        vytvor.close()


    def main():
        stud_subject()
        writing()
        firefox.open('vysvedcenie_test.html')

    if __name__ == '__main__':
        main()

Recommended Answers

All 2 Replies

a simple table:

subjects = [ 'maths' , 'fyzics' , 'history' ]
header = "<h4>Study Subjects</h4><table>"
footer = "</table>"

def write():
    f = open( 'vysvedcenie_test.html' , 'w' )
    f.write(header)
    for subject in subjects:
        body = "<tr><td>%s</td></tr>"%subject
        f.write(body)
    f.write(footer)
    f.close()

You have to append a newline, "\n"

    for record in ['<table border="1" cellpadding="15" cellspacing="5" width="100%">',
                   '<tr>',
                   '<th colspan="2">Vysvedcenie</th>',
                   '</tr>',
                   '<tr>',
                   '<td width="50%">Meno, Priezvisko:</td>\<td>Skolsky rok:</td>',
                   '</tr>']
        ## number of lines truncated above           
        vytvor.write("%s\n" % (record))
commented: it's nice solution! +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.