Utkarsh Agrawal
Utkarsh Agrawal

Reputation: 87

How to write for loop content in html code python?

Below is the code

urls.append('http://google.com')
urls.append('http://stacoverflow.com')
 whole = """<html>
<head>
<title>output -</title>
</head>
<body>Below are the list of URLS
%s    // here I want to write both urls.
</body>
</html>"""
for x in urls:
    print x
f = open('myfile.html', 'w')
f.write(whole)
f.close()

So this is the code for saving the file in HTML format. But I can't find the way to get the contents of for loop into HTML file. In other words, I want to write a list of indexes elements i.e. http://google.com, http://stackoverflow.com into my HTML file. As you can see that I have created myfile.html as HTML file, So I want to write both URLs which are in the list of indexes into my HTML file

Hope this time I better explain? How can I? Would anyone like to suggest me something? It would be a really big help.

Upvotes: 0

Views: 958

Answers (1)

Andersson
Andersson

Reputation: 52675

Try below code:

urls.append('http://google.com')
urls.append('http://stacoverflow.com')
whole = """<html>
<head>
<title>output -</title>
</head>
<body>Below are the list of URLS
%s
</body>
</html>"""
f = open('myfile.html', 'w')
f.write(whole % ", ".join(urls))
f.close()

Upvotes: 1

Related Questions