Python Forum
Word replace with string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Word replace with string (/thread-16075.html)



Word replace with string - dabozz - Feb-13-2019

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


RE: Word replace with string - ichabod801 - Feb-13-2019

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.