Python Forum
Add new line after finding last string in a region
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add new line after finding last string in a region
#1
Hello

I have this input .txt file (a demo of it here, on a small scale) that after running the Python 3.6 code it should write in the input the following output (please see the output interleaved with #):

Output:
Noclue *Title Test 12125 124125 asdas 1 1 1 1 rthtr 1 1 1 1 asdasf 1 1 1 1 asfasf 1 1 1 1 blabla 1 1 1 1 #Expected "*Title Test2" here 124124124 *Title Dunno 12125 124125 12763125 1 1 1 1 whatever 1 1 1 1 #Expected "*Title Dunno2" here 214142122 #and so on for thousands of them..
I have this code, that I have to adapt:

index = 0
insert = False
currentTitle = ""
testfile = open("test.txt","r")    
content = testfile.readlines()
finalContent = content
testfile.close()
# Should change the below line of code I guess to adapt
#titles = ["TitleX","TitleY","TitleZ"]   

for line in content:
    index = index + 1
    for title in titles:
        if line in title+"\n":    
            currentTitle = line
            print (line)
    if line == "1 1 1 1\n":
        insert = True
    if (insert == True) and (line != "1 1 1 1\n"):
        finalContent.insert(index-1, currentTitle[:6] + "2" + currentTitle[6:])
        insert = False

f = open("test.txt", "w")
finalContent = "".join(finalContent)
f.write(finalContent)
f.close()
Any help on how to adapt the above code to display the desired output would be more than appreciated!
Thanks!
Reply
#2

  1. read input line
  2. write line to output
  3. If this is target line, write new data
  4. Continue loop at step 1 until finished, but eliminate step 3
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,389 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Using locationtagger to extract locations found in a specific country/region lord_of_cinder 1 1,221 Oct-04-2022, 12:46 AM
Last Post: Larz60+
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,305 Sep-27-2022, 01:38 PM
Last Post: buran
  Finding First Digits in String giddyhead 4 1,323 Aug-17-2022, 08:12 PM
Last Post: giddyhead
  Inserting line feeds and comments into a beautifulsoup string arbiel 1 1,144 Jul-20-2022, 09:05 AM
Last Post: arbiel
Question Finding string in list item jesse68 8 1,798 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  append a string to a modified line Mr_Blue 10 3,727 Sep-16-2021, 07:24 PM
Last Post: Mr_Blue
  How to capture string from a line to certain line jerald 1 1,875 Jun-30-2021, 05:13 PM
Last Post: Larz60+
  How to create new line '/n' at each delimiter in a string? MikeAW2010 3 2,771 Dec-15-2020, 05:21 PM
Last Post: snippsat
  How to print string multiple times on new line ace19887 7 5,568 Sep-30-2020, 02:53 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020