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
  Python Speech recognition, word by word AceScottie 6 7,979 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  filter just with the string word jacklee26 2 414 Feb-03-2020, 03:25 PM
Last Post: snippsat
  Reverse the string word sneha 2 519 Dec-12-2019, 03:37 AM
Last Post: sneha
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 1,517 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019
  print a word after specific word search evilcode1 8 511 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 595 Oct-17-2019, 03:04 PM
Last Post: baquerik
  Replace string in many files in a folder metro17 8 702 Oct-16-2019, 06:46 PM
Last Post: ndc85430
  open, read and replace a string in a file Reims 0 383 Oct-02-2019, 01:30 PM
Last Post: Reims
  Read each line, replace string and save into a new file igormonteiro 2 637 Sep-15-2019, 01:24 PM
Last Post: buran
  replace white space with a string, is this pythonic? Skaperen 1 450 Jun-18-2019, 11:36 PM
Last Post: metulburr

Forum Jump:

Users browsing this thread: 1 Guest(s)