mardi 24 février 2015

Python search and replace not working

I have the following simple HTML file.



<html data-noop=="http://ift.tt/lH0Osb">
<head>
<title>Hello World</title>
</head>
<body>
SUMMARY1
hello world
</body>
</html>


I want to read this into a python script and replace SUMMARY1 with the text "hi there" (say). I do the following in python



with open('test.html','r') as htmltemplatefile:
htmltemplate = htmltemplatefile.read().replace('\n','')

htmltemplate.replace('SUMMARY1','hi there')
print htmltemplate


The above code reads in the file into the variable htmltemplate. Next I call the replace() function of the string object to replace the pattern SUMMARY1 with "hi there". But the output does not seem to search and replace SUMMARY1 with "hi there". Here is what I'm getting.



<html data-noop=="http://ift.tt/lH0Osb"><head><title>Hello World</title></head><body>SUMMARY1hello world</body></html>


Could someone point out what I'm doing wrong here?


Aucun commentaire:

Enregistrer un commentaire