0

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)       
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
binav123
  • 15
  • 2
  • There are too many parts of your code that are missing to reproduce / identify the problems you're having, and possibly too many problems for one single question. Please edit your post to add all the relevant code (and only the relevant code) so anyone can copypaste and run it and have exactly the same issues as you. – bruno desthuilliers Oct 20 '17 at 12:33

2 Answers2

0

Missing parenthesis in the 4th call of replace(). Also, the local html_code should better be returned as the result of the function.

Also, if the first line inside the function is supposed to be a docstring, you should make it a string literal inside a pair of """ """ and give it the same indentation as the rest of the code.

mportes
  • 1,589
  • 5
  • 13
0

You could (doesn't mean that you should) use zip:

titles = ['HI', 'HELLO']
urls = ['url1', 'url2']
descriptions = ['desc1', 'desc2']
links = ['link1', 'link2']
dates = ['date1', 'date2']

for title, url, description, link, date in zip(titles, urls, descriptions, links, dates):
    html_template = html_template.replace('***TITLE***', title, 1)
    html_template = html_template.replace('***IMGURL***', url, 1)
    html_template = html_template.replace('***DESCRIPTION***', description, 1)
    html_template = html_template.replace('***LINK***', link, 1)
    html_template = html_template.replace('***PUBDATE***', date, 1)

print(html_template)
"""<!DOCTYPEhtml>
   <html>
       <head>
           <title>News Archive</title>
       </head>

       <body> 
           <br>
           <ol>

           <!--  Article 1 -->

           <h1>HI</h1>

           <img src=url1>  
           <p>desc1</p>
           <p><strong> Full story: </strong> link1 </p>
           <p><strong> Dateline: </strong> date1 </p>
           <br/>

           <!--  Article 2 -->  
           <h1>HELLO</h1>
           <img src=url2>

           <p>desc2</p>

           <p><strong> Full story: </strong> link2 </p>
           <p><strong> Dateline: </strong> date2 </p>

      </body>
  </html>


What you should do is use a proper HTML template engine (no, I'm not even going to suggest regex).

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • This did it correctly for me :D! Only thing is that we havent learned about zip so i may have to ask my teacher if im allowed to use that :( – binav123 Oct 20 '17 at 12:54