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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  print number of a list line per line lateublegende 2 277 Mar-20-2019, 04:07 PM
Last Post: lateublegende
  xml replacement with python josesalazmit 3 232 Feb-24-2019, 07:28 PM
Last Post: stullis
  Adding new line in a one line txt file. Mady 7 565 Nov-21-2018, 01:04 PM
Last Post: Gribouillis
  Best replacement for pyzmail in lines 15 and 16 Pedroski55 0 357 Nov-03-2018, 06:12 AM
Last Post: Pedroski55
  Add a line after a specific line morgandebray 3 464 Aug-07-2018, 12:12 PM
Last Post: buran
  Compare element of list with line of file : if match, delete line silfer 4 722 Jul-21-2018, 02:44 PM
Last Post: silfer
  Print Nested List Line by Line anelliaf 5 1,628 May-23-2018, 06:54 PM
Last Post: buran
  Is pathlib a viable replacement for os.path? j.crater 4 3,575 Jan-13-2018, 09:49 AM
Last Post: Gribouillis
  Line by line execution Filthy_McNasty 1 895 Dec-11-2017, 10:35 PM
Last Post: wavic
  How to change language line by line? dialbrocom 2 1,003 Sep-28-2017, 08:09 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)