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"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With