By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,473 Members | 3,292 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,473 IT Pros & Developers. It's quick & easy.

Use Python to write HTML file

P: 34
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!
Sep 17 '07 #1
Share this Question
Share on Google+
8 Replies


P: 34
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!
Sep 17 '07 #2

bartonc
Expert 5K+
P: 6,596
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:
Expand|Select|Wrap|Line Numbers
  1. #create the file (f)
  2. f = open("pathAndFileName", 'w')
  3. #  write the data
  4. f.write(theString)
  5. #  close the file
  6. r.close()
Sep 17 '07 #3

P: 34
Is this right/ok?

f = open(f, "w")

f.write('<!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/displayEventLists.css" type="text/css">' + "\n"
)

f.close()
Sep 17 '07 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Is this right/ok?

f = open(f, "w")

f.write('<!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/displayEventLists.css" type="text/css">' + "\n"
)

f.close()
That should work, but this is better:
Expand|Select|Wrap|Line Numbers
  1. f = open(file_name, "w")
  2.  
  3. f.write('\n'.join(['<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
  4.                    '<html>',
  5.                    ' <head>',
  6.                    ' <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">',
  7.                    ' <title>TITLE</title>',
  8.                    ' <link rel="stylesheet" href="css/displayEventLists.css" type="text/css">'
  9.                    ])
  10.         )
  11.  
  12. f.close()
Sep 17 '07 #5

bartonc
Expert 5K+
P: 6,596
That should work, but this is better:
Expand|Select|Wrap|Line Numbers
  1. f = open(file_name, "w")
  2.  
  3. f.write('\n'.join(['<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
  4.                    '<html>',
  5.                    ' <head>',
  6.                    ' <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">',
  7.                    ' <title>TITLE</title>',
  8.                    ' <link rel="stylesheet" href="css/displayEventLists.css" type="text/css">'
  9.                    ])
  10.         )
  11.  
  12. f.close()
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:
Expand|Select|Wrap|Line Numbers
  1. f = open(file_name, "w")
  2.  
  3. f.writelines(['<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n',
  4.                    '<html>\n',
  5.                    ' <head>\n',
  6.                    ' <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">\n',
  7.                    ' <title>TITLE</title>\n',
  8.                    ' <link rel="stylesheet" href="css/displayEventLists.css" type="text/css">\n'
  9.                    ])
  10.  
  11. f.close()
Sep 17 '07 #6

P: 15
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
Expand|Select|Wrap|Line Numbers
  1. f.write("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">""")
  2.  
Sep 19 '07 #7

ilikepython
Expert 100+
P: 844
Use triple double-quoted strings
Expand|Select|Wrap|Line Numbers
  1. f.write("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">""")
  2.  
Yes, I think that is good because everything is preserved.
Expand|Select|Wrap|Line Numbers
  1. html = """
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  3. etc...
  4. """
  5. f.write(html)
  6.  
Also, a good idea is to use string formatting to fill out certain values:
Expand|Select|Wrap|Line Numbers
  1. html = """
  2. <table>
  3.     %s
  4. </table>
  5. """
  6.  
  7. theTable = "<tr>"     # start row
  8. for x in range(4):
  9.     theTable += "<td>%d</td>" % x     # individual datas
  10. theTable += "</tr>"   # end row
  11.  
  12. f.write(html % theTable)
  13.  
Sep 19 '07 #8

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

Post your reply

Sign in to post your reply or Sign up for a free account.