Extract line from txt file using python

import re

with open('text.txt') as input_data:
    lines = input_data.readlines()
    re_number = re.compile("\+\d\d \d\d\d\d \d\d\d\d")
    number = "+99 9999 9999"
    blocks = []
    tmp_block = []
    for index, line in enumerate(lines):
        if (re_number.search(line)):
            if (tmp_block):
                blocks.append(tmp_block.copy())
                tmp_block.clear()
                flag = 0
            if (number in line):
                flag = 1
        if (flag):
            tmp_block.append(line)
    if (flag):
        blocks.append(tmp_block.copy())

print (blocks)

Here's your answer:

number = "+99 9999 9999"
with open('text.txt') as input_data:
    lines = input_data.readlines()

    # Instead of looping over the lines, we
    # loop over an array of integers starting
    # from zero and ending at the (number of
    # lines in the file minus 1).
    # (Remember,python lists are zero indexed, thats why)
    for line_no in range(len(lines)):
        if number in lines[line_no]:

            # The current line
            print(lines[line_no], end="")

            # Print the next line
            print(lines[line_no+1], end="")

            # And the next one too
            print(lines[line_no+2], end="")

You have the file read portion sorted out. You need to figure out the print statement.

Here's the code to take care of it. For simplicity, i assigned all the data in the file to a variable. Also I modified the input data. The first set has 3 rows for +99 9999 9999

import re

filedata = '''02/09/2020, 23:45 - +99 9999 9999: 02/09/2020
task A -Changes A
task b Changes b
task c Changes c
03/09/2020, 01:55 - +88 8888 8888: 2-SEP-2020
task c -Changes c
task d Changes d
03/09/2020, 01:55 - +99 9999 9999: 2-SEP-2020
task e -Changes e
task f Changes f'''

number = '+99 9999 9999'

for line in filedata.split('\n'):
    z = re.match(r"[+\d{2} \d{4} \d{4}]",line)
    if z: found = number in line
    if found: print (line)

Explanation of the above code:

For each line read, do a reg ex match for +nn nnnn nnnn where n is any digit (d denotes digit). The result is sent to z.

If z has any value, then a match was found. If we found a match, then you want to find out if the line is +99 9999 9999 or some other number pattern.

If the pattern matches, then you set the flag to found. If the flag is found, then print the line. Continue printing the line until the next set of +nn nnnn nnnn line is found. When found, check if it is +99 9999 9999. If it is not, then turn the flag to False. The condition found = number in line results in True or False. When the flag is False, we know a different set has started. Stop printing the lines.

Hope this explains. If you still have questions on the logic, let me know.

The output of this will be:

02/09/2020, 23:45 - +99 9999 9999: 02/09/2020
task A -Changes A
task b Changes b
task c Changes c
03/09/2020, 01:55 - +99 9999 9999: 2-SEP-2020
task e -Changes e
task f Changes f

This will work irrespective of how many rows you have between +99 9999 9999 and the next set of +nn nnnn nnnn where n can be any digit.

Here's the code you need with file read:

import re
number = "+99 9999 9999"
with open('text.txt') as input_data:
    for line in input_data:
        z = re.match(r"[+\d{2} \d{4} \d{4}]",line)
        if z: found = number in line
        if found: print (line)

I am making some wild guesses on what you are trying to do here.

Let's assume you want to find John +99 9999 9999 as a string in the file and print all the lines associated to this. Then here's the code.

import re
filedata = '''02/09/2020, 23:45 - John +99 9999 9999: 02/09/2020
task A -Changes A
task b Changes b
task c Changes c
03/09/2020, 01:55 - Suzan +88 8888 8888: 2-SEP-2020
task c -Changes c
task d Changes d
03/09/2020, 01:55 - Thomas +99 9999 9999: 2-SEP-2020
task e -Changes e
task f Changes f'''

name = 'John'
for line in filedata.split('\n'):
    z = re.findall(r"\w+ \+\d{2} \d{4} \d{4}",line)
    if z: found = (name in line) and (line[:4] != 'task')
    if found: print (line)

The output of this will be:

02/09/2020, 23:45 - John +99 9999 9999: 02/09/2020
task A -Changes A
task b Changes b
task c Changes c

This will work for the following patterns of code:

02/09/2020, 23:45 - John , Salesman +99 9999 9999: 02/09/2020

02/09/2020, 23:45 - John Salesman +99 9999 9999: 02/09/2020

Let me know what you are trying to find. Hopefully all these examples should help you get what you are looking for.

Based on the new data you shared, here's the code:

filedata = """[23/9/20, 11:26:42 PM] John - Salesman: 23/09/2020
-task a
-task b
[23/9/20, 11:30:03 PM] Shawn - Support: 23/09/2020
-task c
-task d
[24/9/20, 9:54:44 PM]Shawn - Support: 24/09/2020
-task e
-task f
[24/9/20, 10:06:58 PM] Damien - Support: 24/09/2020
-task g
-task h
-task i
-task j
[24/9/20, 10:53:52 PM] John - Salesman: 24/09/2020
-task k
-task l
-task m
-task n"""

import re
name = 'John - Salesman'
for line in filedata.split('\n'):
    z = re.findall(r"([\w+ \- \w+:]*\d{2}\/\d{2}\/\d{4})",line)
    if z: found = (name in line) and (line[:4] != 'task')
    if found: print (line)

The output of this will be:

[23/9/20, 11:26:42 PM] John - Salesman: 23/09/2020
-task a
-task b
[24/9/20, 10:53:52 PM] John - Salesman: 24/09/2020
-task k
-task l
-task m
-task n

In case you want to play around with the regex expression, you can try it out here regEx expression


Because your code just find the line which contains number you wanted, you can set a flag to print following content until another number:

if __name__ == '__main__':
    number = "+99 9999 9999"
    task = 'task'
    wanted = False
    with open('text.txt') as input_data:
        for line in input_data:
            if number in line or wanted:
                wanted = True
                print(line.strip())
            if line[:4] != task and number not in line:
                wanted = False

Tags:

Python