I'm trying to convert a XML file to HTML using python. We have the .css file that contains the codes for the format of the output. We have been trying to run the following code:
def main():
infile = open("WTExcerpt.xml", "r", encoding="utf8")
headline=[]
text = infile.readline()
outfile = open("DemoWT.html", "w")
print("<html>\n<head>\n<title>Winter's Tale</title>\n",file=outfile)
print("<link rel='stylesheet' type='text/css' href='Shakespeare.css'>\n</head>\n<body>\n",file=outfile)
while text!="":
#print(text)
text = infile.readline()
text = text.replace("<w>", "")
if "<title>" in text and "</title>" in text:
print("<h1>",text,"</h1>\n",file=outfile)
elif text=="<head>":
while text!="</head>":
headline.append(text)
print("<h3>headline<\h3>\n",file=outfile)
main()
but we don't know how to make Python read "text" and "headline" as our variables (changing with every time the loop is executed) instead of a pure string. Do you have any idea? Thank you very much.
with open(filename, "r") as f: for line in f: ...rather thatopen()andreadline, that you can add the contents ofheadlineto anh3element by writing"<h3>{}</h3>\n".format(" ".join(headline)), etc. But really, why not just use an actual XML parsing module?