Paste text into text file following specific text using Python

I need to edit some text files to include new information, but I will need to paste this information into specific places in the file based on the surrounding text.

This does not work the way I need:

with open(full_filename, "r+") as f: lines = f.readlines() for line in lines: if 'identifying text' in line: offset = f.tell() f.seek(offset) f.write('Inserted text') 

... in that it adds text to the end of the file. How do I write it on the next line after the identification text?

(AFAICT, this is not a duplicate of such questions, since none of them could answer this question)

+4
source share
3 answers

If you don't need to work in place, perhaps something like:

 with open("old.txt") as f_old, open("new.txt", "w") as f_new: for line in f_old: f_new.write(line) if 'identifier' in line: f_new.write("extra stuff\n") 

(or to be compatible with Python-2.5):

 f_old = open("old.txt") f_new = open("new.txt", "w") for line in f_old: f_new.write(line) if 'identifier' in line: f_new.write("extra stuff\n") f_old.close() f_new.close() 

which turns

 >>> !cat old.txt a b c d identifier e 

in

 >>> !cat new.txt a b c d identifier extra stuff e 

(The usual warning about using 'string1' in 'string2': 'name' in 'enamel' is True, 'hello' in 'Othello' is True, etc., but obviously you can make the condition arbitrarily complex.)

+6
source

You can use regex and then replace text.

 import re c = "This is a file contents, apparently you want to insert text" re.sub('text', 'text here', c) print c 

returns "This is the contents of the file, apparently you want to paste the text here"

Not sure if it will work for your usecase, but it is nice and simple if it fits.

+1
source

This will look for any line in the file (not specific) that will only be at the beginning of the line, i.e. may span multiple lines.)

You can usually follow the algorithm like:

  • search for a line in a file and record "location"
  • then split the file about this "location" and try to create new files as
    • write source to a new file
    • next, write your "NEW TEXT" to a new file
    • next, add content to the new file

Let's see the code:

 #!/usr/bin/python import os SEARCH_WORD = 'search_text_here' file_name = 'sample.txt' add_text = 'my_new_text_here' final_loc=-1 with open(file_name, 'rb') as file: fsize = os.path.getsize(file_name) bsize = fsize word_len = len(SEARCH_WORD) while True: found = 0 pr = file.read(bsize) pf = pr.find(SEARCH_WORD) if pf > -1: found = 1 pos_dec = file.tell() - (bsize - pf) file.seek(pos_dec + word_len) bsize = fsize - file.tell() if file.tell() < fsize: seek = file.tell() - word_len + 1 file.seek(seek) if 1==found: final_loc = seek print "loc: "+str(final_loc) else: break # create file with doxygen comments f_old = open(file_name,'r+') f_new = open("new.txt", "w") f_old.seek(0) fStr = str(f_old.read()) f_new.write(fStr[:final_loc-1]); f_new.write(add_text); f_new.write(fStr[final_loc-1:]) f_new.close() f_old.close() 
+1
source

All Articles