Home > Mobile >  How do I use python 3 to find a certain text line and copy the lines below it?
How do I use python 3 to find a certain text line and copy the lines below it?

Time:12-21

I want to make a program in python 3 that does the following but I'm not sure how. Basically I want a python 3 script that reads a txt document and looks for a certain phrase in each text line (EG: Example Phrase). Then if it fines a line with a matching phrase it would copy the text from the two lines below it and combine them with a ":" and save it to a new txt file.

Example:

Example Phrase
Text Line One
Text Line Two
Text Line One:Text Line Two

If anyone could help me make this that would be amazing!

Thanks for all the help in advance, SpaceBurn

Edit: I would like to mention that the example phrase would not be the only thing in the text line (EG: this is some Example Phrase text). If the program sees a line with "Example Phrase" in it would the copy the two line below it and combine it with a ":" like mentioned before.

CodePudding user response:

You can use find() method and loop over the lines you read from a file. When you reach the line with the phrase, collect the index of the line and break from the loop. Afterwards, join the lines with join() method and do not forget to start from the line line_id 1 in order to collect the lines after the line with your phrase.

text_lines = open("my_file.txt", "r").readlines()
for i_, line in enumerate(lines):
    if line.find("my_phrase")!=-1:
        line_id = i_
        break

joined_lines = ":".join(lines[line_id 1:)

out_file = open("my_output.txt", "w")
out_file.write(joined_lines)

Be careful. This will make a file with a single line in the output file. Not sure what you want to do with these later.

CodePudding user response:

You could use a regex to match your pattern and join the next lines:

import re

pattern = 'Example Phrase'
regex = pattern   '[^\n]*\n([^\n]*)\n([^\n]*)\n?'

with open('input.txt', 'r') as a, open('output.txt', 'w') as b:
    for m in re.finditer(regex, a.read()):
        b.write(':'.join(m.groups()) '\n')

NB. I'm typing on a phone, I couldn't test the code.

CodePudding user response:

This approach can take big input file as it will read line by line from input file, record the text to search and handle text lines below it. It can also handle successive text to search. Light comment is included in the code.

Code

def mysearcher(infn, outfn, text_search):
    """
    Read infn file and find text_search, if found write the the 2 lines below it, to outfn file.
    """   
    found = 0  # counter if text_search is found
    ret = []  # temp storage

    with open(outfn, 'w') as w:  # open file for writing overwrite mode
        with open(infn, 'r') as f:  # open file for reading
            for lines in f:
                line = lines.rstrip()

                if found:
                    ret.append(line)  # add new line in the ret list

                    # Write to output if ret list has two elements.
                    if len(ret) == 2:
                        w.write(f'{ret[0]}:{ret[1]}\n')  # write to output

                        # Update ret and found counter.                        
                        found -= 1
                        if found == 0:
                            ret = []  # reset
                        else:
                            # Don't reset ret, there is successive text_search found.
                            ret.pop(0)  # just remove the first element

                if text_search in line:
                    found  = 1


# Start
infn = 'input.txt'
outfn = 'out.txt'
text_search = 'Example Phrase'
mysearcher(infn, outfn, text_search)

Sample input file text content

input.txt

Example Phrase
Text Line One
Text Line Two
New Example Phrase
Example Phrase
Example Phrase I did
Text Line Three
Text Line Four

Output

out.txt

Text Line One:Text Line Two
Example Phrase:Example Phrase I did
Example Phrase I did:Text Line Three
Text Line Three:Text Line Four
  • Related