Bottom Page

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.
ichabod801 wrote Aug-15-2019, 05:50 PM:
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.
Quote
#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

Quote
#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.
Quote
#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))
Quote
#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

Quote
#6
Thanks. One thing.

I just want to write in same myfile.txt file, not the output.txt file.
Quote
#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) 
Quote
#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.
Quote
#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)
buran wrote Nov-11-2019, 12:58 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  xml replacement with python josesalazmit 3 356 Feb-24-2019, 07:28 PM
Last Post: stullis
  Best replacement for pyzmail in lines 15 and 16 Pedroski55 0 448 Nov-03-2018, 06:12 AM
Last Post: Pedroski55
  Is pathlib a viable replacement for os.path? j.crater 4 4,373 Jan-13-2018, 09:49 AM
Last Post: Gribouillis
  Using python for text replacement omar 1 1,560 Dec-22-2016, 01:36 AM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)