2017-10-20 171 views
1

我试图使用python(第一年UNI学生学习蟒蛇的基本概念)如何使用for循环(在函数中)替换字符串中的子字符串的多个实例?

说我有一个HTML模板这样写一个文本文件,网页文件:

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> 
""" 

可以说我要按顺序替换列表中的字符串的所有***TITLE***实例。这里是包含列表字符串:

titles = ['HI', 'HELLO'] 

要与'HI'取代***TITLE***一审和***TITLE***'HELLO'第二种情况下,我会做到这一点:

for t in titles: 
    html_template = html_template.replace('***TITLE***', t, 1) 

但如果我想创建一个for循环(在一个函数中),例如,将***TITLE***替换为包含10个字符串的相应列表,***IMGURL***以及包含10个字符串的相应列表,***DESCRIPTION***以及包含10个字符串的相应列表等等对于其他占位符和它们各自的10个字符串列表?

我试过下面的函数,但IDLE环境表示它不工作:语法错误和解析时Python意外的EOF。出错的地方是:
1. extract_file(file) - 当我尝试在shell窗口中做yo = generate_html('file.html')(file.html是文件名)并打印出来时,shell窗口返回None

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)  
+0

您的代码中有太多部分缺失以重现/识别您遇到的问题,并且可能出现单个问题的问题。请编辑您的帖子以添加所有相关代码(仅限相关代码),以便任何人都可以复制粘贴并运行它,并且与您的问题完全相同。 –

回答

0

可以(并不意味着你应该)使用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> 


你应该做的是使用适当的HTML模板引擎(no, I'm not even going to suggest regex)。

+0

这对我来说是正确的:D!唯一的问题是,我们还没有学习拉链,所以我可能不得不问我的老师,如果我允许使用:( – binav123

0

replace()第四呼叫缺少括号: -
2. for i in image_url:超过:

阅读功能时说,语法错误。此外,本地html_code应该最好作为该函数的结果返回。另外,如果函数中的第一行应该是一个文档字符串,那么应该在一对""""""之内使其成为一个字符串文字,并为其赋予与其余代码相同的缩进。

+0

谢谢!它帮助:D – binav123

相关问题