0

I have the following simple HTML file.

<html data-noop=="http://www.w3.org/1999/xhtml">
<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://www.w3.org/1999/xhtml"><head><title>Hello World</title></head><body>SUMMARY1hello world</body></html>

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

3
  • Because open() doesn't return a string object, it returns a file object. Also, you are only opening the file for reading ('r'). This is a also a duplicate of: stackoverflow.com/questions/39086/… Commented Feb 24, 2015 at 19:13
  • I think the mistake I was doing was in line 3. I changed it to <code>htmltemplate = htmltemplate.replace('SUMMARY1','hi there')</code> and it worked. Commented Feb 24, 2015 at 19:35
  • You also added .read() which wasn't there in your original post and changes a lot. But regardless, you still aren't actually editing the file, 'SUMMARY' will still appear in test.html. Commented Feb 24, 2015 at 21:18

1 Answer 1

2

open() does not return a str, it returns a file object. Additionally, you are only opening it for reading ('r'), not for writing.

What you want to do is something like:

new_lines = []
with open('test.html', 'r') as f:
    new_lines = f.readlines()
with open('test.html', 'w') as f:
    f.writelines([x.replace('a', 'b') for x in new_lines])

The fileinput library makes this a lot easier.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.