Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract line from txt file using python

Tags:

python

I am new here and currently learning python. This is my first post here.

I am trying to extract a chat history sent out by a particular user via .txt file. for example number +99 9999 9999. But I'm unable to get the content in between.

02/09/2020, 23:45 - +99 9999 9999: 02/09/2020
task A -Changes A
task b Changes b
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

My current code is

number = "+99 9999 9999"
with open('text.txt') as input_data:
    for line in input_data:
        if number in line: 
            print(line)

my output is the number with the content

02/09/2020, 23:45 - +99 9999 9999: 02/09/2020
03/09/2020, 01:55 - +99 9999 9999: 2-SEP-2020

How can I edit my code to show the lines after if the number matches the row? Any guidance would be appreciated.

the output that i want

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

New data

[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
like image 668
Fuji Avatar asked Jan 12 '21 07:01

Fuji


People also ask

How do you extract a line from a text file in Python?

To read a text file in Python, you follow these steps: First, open a text file for reading by using the open() function. Second, read text from the text file using the file read() , readline() , or readlines() method of the file object.

How do I read a text file line by line in Python?

Method 1: Read a File Line by Line using readlines() readlines() is used to read all the lines at a single go and then return them as each line a string element in a list. This function can be used for small files, as it reads the whole file content to the memory, then split it into separate lines.

How do you read a single line from a file in Python?

Python readline() is a file method that helps to read one complete line from the given file. It has a trailing newline (“\n”) at the end of the string returned. You can also make use of the size parameter to get a specific length of the line.

How do I extract data from Notepad using Python?

f = open('myTextFile. txt', "r") lines = f. readlines() for line in lines: words = line. split("-") # words is a list (of strings from a line), delimited by "-".


1 Answers

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

like image 97
Joe Ferndz Avatar answered Sep 16 '22 20:09

Joe Ferndz