Python Forum

Full Version: Word replace with string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I would like to replace a word with a variable in a html file.
My code is:
name = raw_input("Name: ")
s = open("helloworld.html").read()
s = s.replace('YourNameHere', name)
f = open("helloworld.html", 'w')
f.write(s)
f.close()
But it is not working..

So if the user add his name, on the html file it should be appear.
For example: YourNameHere should replaced with "John Smith".

The error message:
Traceback (most recent call last):
File "C:/Users/tmate/PycharmProjects/Tesztprojekt/fb.py", line 12, in <module>
s = s.replace('YourNameHere', name)
NameError: name 'name' is not defined

Any suggestion?

Thanks,
Tamas
It's working fine for me. Are you sure name is spelled correctly in your original file?

Also, you should switch to Python 3. At the end of the year support for 2.7 will stop.