0

I know that many questions exist here on finding and replacing text in file using python 2. However being a very new to python, I did not understand the syntax and may be the purpose also will be different.

I am looking for something very simple code lines as in linux shellscript

sed -i 's/find/replace/' *.txt 
sed -i 's/find2/replace2/' *.txt

Can this code work to replace a multiline text

with open('file.txt', 'w') as out_file:
   out_file.write(replace_all('old text 1', 'new text 1'))
   out_file.write(replace_all('old text 2', 'new text 2'))

Also, there seems a problem with getting another newline, which I do not want. Any ideas or help?

gyeox29ns
  • 101
  • 1
  • 3

2 Answers2

3

So, with Python, the easiest thing to do is read all the text from the file into a string. Then perform any necessary replacements using that string. Then write the entire thing back out to the same file:

filename = 'test.txt'

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

text = text.replace('Hello', 'Goodbye')
text = text.replace('name', 'nom')

with open(filename, 'w') as f:
  f.write(text)

The replace method works on any string and replaces any (case-sensitive) match of the first argument with the second. You're reading and writing to the same file, just in two different steps.

David Antaramian
  • 4,145
  • 1
  • 23
  • 16
2

Here is a quick sample. If you want more powerful search/replace you can use regex instead of string.replace

import fileinput
for line in fileinput.input(inplace=True):
    newline = line.replace('old text','new text').strip()
    print newline

Put the above code in a desired file, say sample.py, and assuming your python is in your path you can run as:

python sample.py inputfile

That will replace 'old text' with 'new text' in inputfile. Ofcourse you can pass multiple files as arguments as well. See https://docs.python.org/2/library/fileinput.html

user3885927
  • 3,363
  • 2
  • 22
  • 42
  • If I want to replace 2 different text instances, then is this correct? `newline = line.replace('old text 1','new text 1').strip() newline = line.replace('old text 2','new text 2').strip()` I just wrote 2 lines of text replace commands. – gyeox29ns Nov 06 '14 at 18:55
  • string.replace will replace all instances. If you want to limit how many instances get replaced you can specify another argument with number of instances to replace. See https://docs.python.org/2/library/string.html at the very bottom – user3885927 Nov 06 '14 at 18:58
  • I guess I was not clear what I want to convey. I am not saying about replacing _n_ instances, but saying replacing 2 or more different lines of text (as I shown in `sed` command in question. The link you referred can limit how many times to replace the text. – gyeox29ns Nov 06 '14 at 19:02
  • Sorry, I didn't notice that and yes you can use line.replace twice and then do one print – user3885927 Nov 06 '14 at 19:05
  • is `fileinput` a module? I am not running the program in terminal as you said `python sample.py inputfile` but running inside another python program. In that case I have to read a file. What changes then? – gyeox29ns Nov 07 '14 at 02:30