Use Python to write HTML file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jld730
    New Member
    • Apr 2007
    • 34

    Use Python to write HTML file

    I have been given a snippet of HTML code that I am to use Python to write it out. I am somewhat new to Python, and completely new to HTML, so I'm still unclear on what it is I am supposed to be doing. So please bear with me as I try to figure this out as I go. But first, just starting out, outputting the first line of the HTML already has me lost with all the competing quotes/unquotes. How do I write out this first line using Python?

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    f.write(??????? )

    Thanks!
  • jld730
    New Member
    • Apr 2007
    • 34

    #2
    Never mind -- I finally decided to not complicate it in my mind and tried something simple, and that worked.

    But let me ask this... what is the proper way for do this, with one long write function for 30+ lines of html text, or should you execute the write function once for each line? And, what do you do about the indentation? I can see in the html file given to me that there are indentations that are made up of, for instance, a tab and two spaces. So in quotes do you put a tab and two spaces? I did that and it doesn't seem to give the same depth of indentation. Should I use spaces only? I'm just not sure on how picky html is.

    Thanks for any guidance!

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by jld730
      Never mind -- I finally decided to not complicate it in my mind and tried something simple, and that worked.

      But let me ask this... what is the proper way for do this, with one long write function for 30+ lines of html text, or should you execute the write function once for each line? And, what do you do about the indentation? I can see in the html file given to me that there are indentations that are made up of, for instance, a tab and two spaces. So in quotes do you put a tab and two spaces? I did that and it doesn't seem to give the same depth of indentation. Should I use spaces only? I'm just not sure on how picky html is.

      Thanks for any guidance!
      One chunk is best: for a given string type object (theString), no matter how long -within reason, of course- simply:[CODE=python]#create the file (f)
      f = open("pathAndFi leName", 'w')
      # write the data
      f.write(theStri ng)
      # close the file
      r.close()[/CODE]

      Comment

      • jld730
        New Member
        • Apr 2007
        • 34

        #4
        Is this right/ok?

        f = open(f, "w")

        f.write('<!DOCT YPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">' + "\n" +
        '<html>' + "\n" +
        ' <head>' + "\n" +
        ' <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">' + "\n" +
        ' <title>TITLE</title>' + "\n" +
        ' <link rel="stylesheet " href="css/displayEventLis ts.css" type="text/css">' + "\n"
        )

        f.close()

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by jld730
          Is this right/ok?

          f = open(f, "w")

          f.write('<!DOCT YPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">' + "\n" +
          '<html>' + "\n" +
          ' <head>' + "\n" +
          ' <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">' + "\n" +
          ' <title>TITLE</title>' + "\n" +
          ' <link rel="stylesheet " href="css/displayEventLis ts.css" type="text/css">' + "\n"
          )

          f.close()
          That should work, but this is better:[code=Python]f = open(file_name, "w")

          f.write('\n'.jo in(['<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
          '<html>',
          ' <head>',
          ' <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">',
          ' <title>TITLE</title>',
          ' <link rel="stylesheet " href="css/displayEventLis ts.css" type="text/css">'
          ])
          )

          f.close()[/code]

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by bvdet
            That should work, but this is better:[code=Python]f = open(file_name, "w")

            f.write('\n'.jo in(['<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
            '<html>',
            ' <head>',
            ' <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">',
            ' <title>TITLE</title>',
            ' <link rel="stylesheet " href="css/displayEventLis ts.css" type="text/css">'
            ])
            )

            f.close()[/code]
            While I abhor putting literals in the the argument of a function call like this, I'm in a rush at the moment so I'll just throw out this alternative that does write one line at a time in a single call:[code=Python]f = open(file_name, "w")

            f.writelines(['<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n',
            '<html>\n',
            ' <head>\n',
            ' <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">\n',
            ' <title>TITLE</title>\n',
            ' <link rel="stylesheet " href="css/displayEventLis ts.css" type="text/css">\n'
            ])

            f.close()[/code]

            Comment

            • rogerlew
              New Member
              • Jun 2007
              • 15

              #7
              Originally posted by jld730
              I have been given a snippet of HTML code that I am to use Python to write it out. I am somewhat new to Python, and completely new to HTML, so I'm still unclear on what it is I am supposed to be doing. So please bear with me as I try to figure this out as I go. But first, just starting out, outputting the first line of the HTML already has me lost with all the competing quotes/unquotes. How do I write out this first line using Python?

              <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

              f.write(??????? )

              Thanks!
              Use triple double-quoted strings
              Code:
              f.write("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">""")

              Comment

              • ilikepython
                Recognized Expert Contributor
                • Feb 2007
                • 844

                #8
                Originally posted by rogerlew
                Use triple double-quoted strings
                Code:
                f.write("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">""")
                Yes, I think that is good because everything is preserved.
                [code=python]
                html = """
                <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
                etc...
                """
                f.write(html)
                [/code]
                Also, a good idea is to use string formatting to fill out certain values:
                [code=python]
                html = """
                <table>
                %s
                </table>
                """

                theTable = "<tr>" # start row
                for x in range(4):
                theTable += "<td>%d</td>" % x # individual datas
                theTable += "</tr>" # end row

                f.write(html % theTable)
                [/code]

                Comment

                • jld730
                  New Member
                  • Apr 2007
                  • 34

                  #9
                  Way late, but thanks everyone for the useful responses! I'm good now (3 months later!).

                  Comment

                  Working...