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!
8 12906
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!
bartonc 6,596
Recognized Expert Expert
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: - #create the file (f)
-
f = open("pathAndFileName", 'w')
-
# write the data
-
f.write(theString)
-
# close the file
-
r.close()
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()
bvdet 2,851
Recognized Expert Moderator Specialist
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: - f = open(file_name, "w")
-
-
f.write('\n'.join(['<!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/displayEventLists.css" type="text/css">'
-
])
-
)
-
-
f.close()
bartonc 6,596
Recognized Expert Expert
That should work, but this is better: - f = open(file_name, "w")
-
-
f.write('\n'.join(['<!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/displayEventLists.css" type="text/css">'
-
])
-
)
-
-
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: - 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/displayEventLists.css" type="text/css">\n'
-
])
-
-
f.close()
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 -
f.write("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">""")
-
Use triple double-quoted strings -
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. -
html = """
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
-
etc...
-
"""
-
f.write(html)
-
Also, a good idea is to use string formatting to fill out certain values: -
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)
-
Way late, but thanks everyone for the useful responses! I'm good now (3 months later!).
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: M.E.Farmer |
last post by:
Hello c.l.py!,
I have just finished this and decided to share.
PySourceColor is a module to convert Python source into colored html.
Yes it has been done before, but I like this better:)
You can easily define your own colorscheme.
example usage:
# Highlight PySourceColor.py
python PySourceColor.py
or
# Show help
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |