Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract the file Data in python

I wanted to extract the date from the given string on the basis of tag.

My string is -

DATE: 7/25/2017 DATE OPENED: 7/25/2017 RETURN DATE: 7/26/2017 
NUMBER: 201707250008754 RATE:  10.00

I want something like this - If I give "DATE" it should return 7/25/2017 only

if I give "RETURN DATE" it should return 7/26/2017

if I give the "NUMBER" it should return 201707250008754 and so on.

How we can achieve this in Python 2.7 (Note: Dates and numbers are always random in string"

like image 923
Ravi K Avatar asked Mar 11 '26 11:03

Ravi K


1 Answers

You can create a dictionary from the string's contents with re:

import re
s = 'DATE: 7/25/2017 DATE OPENED: 7/25/2017 RETURN DATE: 7/26/2017 NUMBER: 201707250008754 RATE: 10.00'
results = re.findall('[a-zA-Z\s]+(?=:)|[\d/\.]+', s)
d = dict([re.sub('^\s+', '', results[i]), results[i+1]] for i in range(0, len(results), 2))
for i in ['DATE', 'RETURN DATE', 'NUMBER']:
   print(d[i])

Output:

7/25/2017
7/26/2017
201707250008754
like image 99
Ajax1234 Avatar answered Mar 13 '26 01:03

Ajax1234



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!