I'm trying to write a text file web document using python (First year uni student learning the basic concepts of python)
Say I have a HTML template like this:
html_template = """<!DOCTYPEhtml>
<html>
<head>
<title>News Archive</title>
</head>
<body>
<br>
<ol>
<!-- Article 1 -->
<h1>***TITLE***</h1>
<img src=***IMGURL***
<p>***DESCRIPTION***</p>
<p><strong> Full story: </strong> ***LINK*** </p>
<p><strong> Dateline: </strong> ***PUBDATE*** </p>
<br/>
<!-- Article 2 -->
<h1>***TITLE***</h1>
<img src=***IMGURL***
<p>***DESCRIPTION***</p>
<p><strong> Full story: </strong> ***LINK*** </p>
<p><strong> Dateline: </strong> ***PUBDATE*** </p>
</body>
</html>
"""
Lets say I want to replace all instances of ***TITLE***
with strings in a list in order. Here is the list containing strings:
titles = ['HI', 'HELLO']
To replace the first instance of ***TITLE***
with 'HI'
and the second instance of ***TITLE***
with 'HELLO'
, I would do this:
for t in titles:
html_template = html_template.replace('***TITLE***', t, 1)
But what if i wanted to create a for loop (in a function) that replaces, for example, ***TITLE***
with a respective list containing 10 strings, ***IMGURL***
with a respective list containing 10 strings, ***DESCRIPTION***
with a respective list containing 10 strings, and so on for the rest of the placeholders and their respective 10 string lists?
I've tried the function below but the IDLE environment said it doesnt work: syntax error and Python unexpected EOF while parsing. The places where it went wrong are:
1. extract_file(file)
-when i try to test it in shell window by doing yo = generate_html('file.html')
(file.html is name of file) and printing out yo, the shell window returns None
.
2. for i in image_url:
- says syntax error when reading over :
The function:
def html_extract(file):
extract_file(file) (calls the respective lists for respective placeholder)
for t in titles:
html_code = html_template.replace('***TITLE***', t, 1)
for i in image_url:
html_code = html_code.replace('***IMGURL***', i, 1)
for d in descriptions:
html_code = html_code.replace('***DESCRIPTION***', d, 1)
for l in links:
html_code = html_code.replace'***LINK***', l, 1)
for p in pubdates:
html_code = html_code.replace('***PUBDATE***', p, 1)