Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
line replacement help
#1
Hi,
I have wrote and trying to implement the code. I want to replace the whole lline containing anything like mov9, inp9 etc.
filename = 'myfile.txt'

with open(filename, 'r') as f:
  text = f.read()

text = text.replace('mov9=', 'mov9=this is')
text = text.replace('inp9=go', 'nom')
text = text.replace('nin9', 'ten9=pop')


with open(filename, 'w') as f:
  f.write(text)
when I run the code..I got the output like 'mov9=this is' first time. But second time I got like' mov9=this is this is'. I just want to replace the whole line containing move9 or inp9 everytime.

Please help me.
Reply


Messages In This Thread
line replacement help - by mdalireza - Aug-15-2019, 05:22 PM
RE: line replacement help - by ichabod801 - Aug-15-2019, 05:55 PM
RE: line replacement help - by mdalireza - Aug-15-2019, 06:22 PM
RE: line replacement help - by ThomasL - Aug-15-2019, 07:46 PM
RE: line replacement help - by ichabod801 - Aug-15-2019, 09:30 PM
RE: line replacement help - by mdalireza - Aug-15-2019, 09:57 PM
RE: line replacement help - by ThomasL - Aug-16-2019, 01:42 PM
RE: line replacement help - by snippsat - Aug-16-2019, 01:04 PM
RE: line replacement help - by mdalireza - Nov-11-2019, 12:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  replacement for fpformat.extract GbSig1998 4 225 Apr-12-2024, 06:15 PM
Last Post: deanhystad
  String replacement in DB WJSwan 0 752 Dec-28-2022, 05:31 AM
Last Post: WJSwan
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 6,352 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  Extract the largest value from a group without replacement (beginner) preliator 1 2,077 Aug-12-2020, 01:56 PM
Last Post: DPaul
  Simple automated SoapAPI Call with single variable replacement from csv asaxty 1 2,102 Jun-30-2020, 06:38 PM
Last Post: asaxty
  xml replacement with python josesalazmit 3 8,382 Feb-24-2019, 07:28 PM
Last Post: stullis
  Best replacement for pyzmail in lines 15 and 16 Pedroski55 0 2,491 Nov-03-2018, 06:12 AM
Last Post: Pedroski55
  Is pathlib a viable replacement for os.path? j.crater 4 9,966 Jan-13-2018, 09:49 AM
Last Post: Gribouillis
  Using python for text replacement omar 1 3,836 Dec-22-2016, 01:36 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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