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?

broccoli
  • 4,738
  • 10
  • 42
  • 54
  • 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: http://stackoverflow.com/questions/39086/search-and-replace-a-line-in-a-file-in-python – dursk Feb 24 '15 at 19:13
  • I think the mistake I was doing was in line 3. I changed it to htmltemplate = htmltemplate.replace('SUMMARY1','hi there') and it worked. – broccoli Feb 24 '15 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`. – dursk Feb 24 '15 at 21:18

1 Answers1

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.

dursk
  • 4,435
  • 2
  • 19
  • 30