1

I need to extract a date from a jpeg format, I have extracted the text from the jpeg in the form of a string & have used regex to extract the date,

Text from JPEG

Cont:7225811153; BillNo4896TableNoR306 07-Jun-201921:18:40

Code used

Importing regular expression & Date time

import re as r

from datetime import datetime

regex to identify the date in the above string

id = r.search(r'\d{2}-\w{3}-\d{4}',text)
print(id)

Output re.Match object; span=(89, 100), match='07-Jun-2019'

However after performing the above code i tried the following to extract the date

Code

Extracting the date

date = datetime.strptime(id.group(),'%d-%B-%Y').date()

Output

ValueError: time data '07-Jun-2019' does not match format '%d-%B-%Y'

Where am I going wrong, or is there a better way to do the same. Help would be really appreciated

Community
  • 1
  • 1
Sid
  • 163
  • 7
  • 1
    Try with `%d-%b-%Y`. `%B` is for "Month as locale’s full name" where as `%b` is for "Month as locale’s abbreviated name." [See Documentation for more info](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes) – Abdul Niyas P M Nov 29 '19 at 11:47

2 Answers2

1

Use %b instead of %B, but make sure you only try to convert the match if it occurred:

import re as r
from datetime import datetime
text = 'Cont:7225811153; BillNo4896TableNoR306 07-Jun-201921:18:40'
id = r.search(r'\d{2}-\w{3}-\d{4}',text)
if id:  # <-- Check if a match occurred
    print(datetime.strptime(id.group(),'%d-%b-%Y').date())
# => 2019-06-07

See the Python demo online

See more details on the datetime.strptime format strings.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

You had it almost perfect. Just replace the B with b.

>>> datetime.strptime(id.group(),'%d-%b-%Y').date()
datetime.date(2019, 6, 7)
Eamonn Kenny
  • 1,926
  • 18
  • 20