Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Word replace with string

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')
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/", line 12, in <module>
s = s.replace('YourNameHere', name)
NameError: name 'name' is not defined

Any suggestion?

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.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to replace on char with another in a string? korenron 3 544 Dec-03-2020, 07:37 AM
Last Post: korenron
  Python Speech recognition, word by word AceScottie 6 10,640 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  filter just with the string word jacklee26 2 899 Feb-03-2020, 03:25 PM
Last Post: snippsat
  Reverse the string word sneha 2 1,044 Dec-12-2019, 03:37 AM
Last Post: sneha
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 3,510 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019
  print a word after specific word search evilcode1 8 1,649 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  How do you replace a word after a match from a list of words in each line of a file? vijju56 1 1,536 Oct-17-2019, 03:04 PM
Last Post: baquerik
  Replace string in many files in a folder metro17 8 1,472 Oct-16-2019, 06:46 PM
Last Post: ndc85430
  open, read and replace a string in a file Reims 0 746 Oct-02-2019, 01:30 PM
Last Post: Reims
  Read each line, replace string and save into a new file igormonteiro 2 1,383 Sep-15-2019, 01:24 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020