Python Forum
Need help to extract particular string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help to extract particular string
#1
Hi,

I want to extract particular string in a file.suppose file contains the following string.

INFO 2019-02-19 20:59:56,178 - Process Start time : 2019-02-19 20:55:09.778793
INFO 2019-02-19 20:59:56,178 - Process End time : 2019-02-19 20:59:56.178893

Output should be like as below.

Output:
Process Start time : 2019-02-19 20:55:09.778793 Process End time : 2019-02-19 20:59:56.178893
I tried to achieve the output using below but not succeeded.

import re
    re.match("(.*)Process Start time(.*)", line):
        c= line.split("-")[3]
        print(c)
Could you please help me in advance
Reply
#2
I think you need an if before the second line. But that won't get the end time. You probably just want to check for 'Process'. In fact, a regular expression is overkill for this. You can just use if 'Process' in line:.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Hi,

It is not working which i tried as below

import re
re.match("(.*)Process Start time(.*)", line):
c= line.split("-")[3]
print©


output:
======
Process Start time : 2019


In date format hyphen(-) is exists so that is the reason it is extracting only year.

Thanks in advance
Reply
#4
Use Code tag as you have gotten info about.

If doing it with regex it could be like this.
import re

data = '''\
INFO 2019-02-19 20:59:56,178 - Process Start time : 2019-02-19 20:55:09.778793
INFO 2019-02-19 20:59:56,178 - Process End time : 2019-02-19 20:59:56.178893'''

>>> r = re.findall(r"P.*", data)
>>> r
['Process Start time : 2019-02-19 20:55:09.778793',
 'Process End time : 2019-02-19 20:59:56.178893']

>>> print('\n'.join(r))
Process Start time : 2019-02-19 20:55:09.778793
Process End time : 2019-02-19 20:59:56.178893

# Without regex
>>> for line in data.split('\n'):
...     line.split(' - ')[::-2]
    
['Process Start time : 2019-02-19 20:55:09.778793']
['Process End time : 2019-02-19 20:59:56.178893']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract substring from a string before a word !! evilcode1 3 532 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Extract a string between 2 words from a text file OscarBoots 2 1,867 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  How to extract specific key value pair from string? aditi06 0 2,520 Apr-15-2021, 06:26 PM
Last Post: aditi06
Question How to extract multiple text from a string? chatguy 2 2,355 Feb-28-2021, 07:39 AM
Last Post: bowlofred
  Extract continuous numeric characters from a string in Python Robotguy 2 2,627 Jan-16-2021, 12:44 AM
Last Post: snippsat
  extract a dictionary from a string berc 4 2,846 Jul-30-2020, 06:58 AM
Last Post: berc
  Extract data from large string pzig98 1 2,124 Jul-20-2020, 12:39 AM
Last Post: Larz60+
  xml.etree.ElementTree extract string values matthias100 2 4,958 Jul-12-2020, 06:02 PM
Last Post: snippsat
  Extract email addresses from string and store them in a list oslosurfer 2 2,687 Nov-24-2019, 03:35 PM
Last Post: oslosurfer
  extract version in a string lokesh 2 2,158 Oct-09-2019, 05:49 AM
Last Post: buran

Forum Jump:

User Panel Messages

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