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
#2
Go line by line:

filename = 'myfile.txt'
 
new_lines = []
with open(filename, 'r') as f:
  for line in f:
    if 'mov9=' in line:
      new_lines.append('mov9=this is')
    elif 'inp9=go' in line:
      new_lines.append('nom')
    elif 'nin9' in line:
      new_lines.append('ten9=pop')
    else:
      new_lines.append(line)
 
with open(filename, 'w') as f:
  f.write('\n'.join(lines))
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks.
I see f.write('\n'.join(lines))

When I compile lines is not found. Is this is 'new_lines'?

If I put the 'new_lines' then the file delete all.
Reply
#4
filename = 'myfile.txt'
  
new_lines = []
with open(filename, 'r') as f:
  for line in f:
    if 'mov9=' in line:
      new_lines.append(line.replace('mov9=', 'mov9=this is'))
    elif 'inp9=go' in line:
      new_lines.append(line.replace('inp9=go', 'nom'))
    elif 'nin9' in line:
      new_lines.append(line.replace('nin9', 'ten9=pop'))
    else:
      new_lines.append(line)
  
with open('output.txt', 'w') as f:
  f.write('\n'.join(new_lines))
Reply
#5
Yes, that should be new_lines. in the last line, not lines.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
Thanks. One thing.

I just want to write in same myfile.txt file, not the output.txt file.
Reply
#7
(Aug-15-2019, 09:57 PM)mdalireza Wrote: I just want to write in same myfile.txt file, not the output.txt file.
There is fileinput in standard library or cleaner use in-place.
import in_place

filename = 'myfile.txt'
new_lines = []
with in_place.InPlace(filename) as f:
    for line in f:
        if 'mov9=' in line:
            new_lines.append(line.replace('mov9=', 'mov9=this is'))
        elif 'inp9=go' in line:
            new_lines.append(line.replace('inp9=go', 'nom'))
        elif 'nin9' in line:
            new_lines.append(line.replace('nin9', 'ten9=pop'))
        else:
            new_lines.append(line)
    f.write(''.join(new_lines) 
Reply
#8
(Aug-15-2019, 09:57 PM)mdalireza Wrote: Thanks. One thing.

I just want to write in same myfile.txt file, not the output.txt file.
Then change the code to do so. What´s the problem in doing so?
I changed it to "output.txt" so that you have your original and the new file
and can compare it visually if the code is doing what you want.
Reply
#9
Hi,
I am trying to write to a list from value from optionmenu. But I am keep getting 'AttributeError: 'tuple' object has no attribute 'get''. Please help

mOption1 = ["True", "False"]
mOpvar1 = tk.StringVar()
mOpvar1.set("True")
# mOpvar1.trace_add('write', self.writevalue)
widget=tk.OptionMenu(self, mOpvar1, *mOption1)
widget.place(x=d+380, y=j+30)
print (widget)
def callback(*args):
dd = mOpvar1.get()
self.widgets.append(args)
mOpvar1.trace('w', callback)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  String replacement in DB WJSwan 0 712 Dec-28-2022, 05:31 AM
Last Post: WJSwan
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,968 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  Extract the largest value from a group without replacement (beginner) preliator 1 2,039 Aug-12-2020, 01:56 PM
Last Post: DPaul
  Simple automated SoapAPI Call with single variable replacement from csv asaxty 1 2,056 Jun-30-2020, 06:38 PM
Last Post: asaxty
  xml replacement with python josesalazmit 3 8,317 Feb-24-2019, 07:28 PM
Last Post: stullis
  Best replacement for pyzmail in lines 15 and 16 Pedroski55 0 2,460 Nov-03-2018, 06:12 AM
Last Post: Pedroski55
  Is pathlib a viable replacement for os.path? j.crater 4 9,909 Jan-13-2018, 09:49 AM
Last Post: Gribouillis
  Using python for text replacement omar 1 3,785 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