Bottom Page

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


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Reverse the string word sneha 2 117 Yesterday, 03:37 AM
Last Post: sneha
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 250 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019
  print a word after specific word search evilcode1 8 185 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 208 Oct-17-2019, 03:04 PM
Last Post: baquerik
  Replace string in many files in a folder metro17 8 354 Oct-16-2019, 06:46 PM
Last Post: ndc85430
  open, read and replace a string in a file Reims 0 146 Oct-02-2019, 01:30 PM
Last Post: Reims
  Read each line, replace string and save into a new file igormonteiro 2 280 Sep-15-2019, 01:24 PM
Last Post: buran
  replace white space with a string, is this pythonic? Skaperen 1 264 Jun-18-2019, 11:36 PM
Last Post: metulburr
  Replace a list or string element with a random one.. pianistseb 3 340 May-09-2019, 08:24 AM
Last Post: buran
  Replace changing string including uppercase character with lowercase character silfer 11 869 Mar-25-2019, 12:54 PM
Last Post: silfer

Forum Jump:

Users browsing this thread: 1 Guest(s)