Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Word replace with string
#1
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
Reply


Messages In This Thread
Word replace with string - by dabozz - Feb-13-2019, 01:29 PM
RE: Word replace with string - by ichabod801 - Feb-13-2019, 03:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Retrieve word from string knob 4 481 Jan-22-2024, 06:40 PM
Last Post: Pedroski55
  extract substring from a string before a word !! evilcode1 3 533 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Replace a text/word in docx file using Python Devan 4 3,302 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 761 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Replace string in a nested Dictianory. SpongeB0B 2 1,194 Mar-24-2023, 05:09 PM
Last Post: SpongeB0B
  Working with Excel and Word, Several Questions Regarding Find and Replace Brandon_Pickert 4 1,540 Feb-11-2023, 03:59 PM
Last Post: Brandon_Pickert
  Replace with upper(string) WJSwan 7 1,583 Feb-10-2023, 10:28 AM
Last Post: WJSwan
  Find and Replace numbers in String giddyhead 2 1,221 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  python-docx regex: replace any word in docx text Tmagpy 4 2,216 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
  Isolate a word from a long string nicocorico 2 1,527 Feb-25-2022, 01:12 PM
Last Post: nicocorico

Forum Jump:

User Panel Messages

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