如何使用python regex从不同类型日期格式的文本中提取日期

问题描述 投票:0回答:2

我正在做ocr问题,我有500张图像,我已经从图像中提取了文本并另存为CSV,在该文本中,日期格式不同,因此我想从该文本中提取日期。请帮助我如何提取日期表单文本。日期格式如下:

07-June-2018,08/3/17,30-04-2018, 03/june/19, sep 29,2018,24may'19

我有这样的文字:

    file_Name            Text
0   01048818.jpeg   
1   0156b46f.jpeg   SST Z9R|.EPS5132K1ZL\nBILL N0 : ZIIG |)T:25HJ'...
2   01a0cabb.jpeg   FourthStreetM|lI\nP174 iirwi iA /\V| Nut\n\n ...
3   02acce30.jpeg   
4   02d54805.jpeg   <§UU[§3U.|HV’\n\nSubway#1b'/raw Mum. 5/4-<5g4....
5   02f4068e.jpeg   \-* 'r"“ \A>~ in u ‘ ..« mu.‘\nR8 Ahv'\ldIh|q...
6   0308aae2.jpeg   server: mm Slalum u\n\nmay I H3533 Dy 7\nIame:...
7   0349ff40.jpeg   m«.mw\\n\n52 M w u1m:m\n\n|‘C.uw1‘Hu w hm‘
8   0358c6c3.jpeg   5\n4 Lu A!rJ././ O 3\n.. I ..Or3 . B _.h H\n, ...
9   037ef967.jpeg   Wemms YUU\n\nEDATHAMARA PETRHLEUM CU\n\nKK ROA...
10  0406cfb1.jpeg   mlu tell us about your visit.\nValidation Code...
11  04887bab.jpeg   202\n\nWy A L/mm LNIRH smwmm, DH A IRH\nM1]! I...
12  049c8dfd.jpeg   M7110
13  04f988a3.jpeg   HAE‘-IGHfi.RPiJRA PLAZA\nFRDH K! [.50 TU MI 220...
14  04fa5e11.jpeg   
15  05f91a79.jpeg   "_‘d”K yous:\n1MCA‘WSter's D911’\n310 S Ilhnoi...
16  068ea6e6.jpeg   Iukul 1:01 cm\n\n,.\n\nMmuy\n\n:1 Mn\nTh‘ ‘\n\...
17  0728f702.jpeg   +1\n\nom INDIAN room\n204 Spring St\nNew York,...
18  077124e4.jpeg   "" "NR\n§u§I' I‘fl»AR'A, ig tun gglilm.’\n\n(~1...
19  093304e6.jpeg   cAsA“:5>”uNL\n\n£N1x'hPR\‘:l‘S\n\nWED\n\n \n\...

我尝试了此代码,但出现了一个错误:

import re
text=df['Text']
m = re.search('\b(\d{2}-\d{2}-\d{4})\.', 'text')
print (m.group(1))

错误:

AttributeError: 'NoneType' object has no attribute 'group'
python regex ocr text-processing
2个回答
0
投票

您可以将dateutil.parserfuzzy=True一起使用以识别不同的日期时间格式

from dateutil.parser import parse
for item in df['Text'].tolist():
    print(parse(item, fuzzy=True))

set dayfirst =如果01/05/09为日期,月份,年格式,则为True,默认值为MDY


0
投票

您可以使用日期查找器模块。

import datefinder
text = "uiuuiiuu 2010-07-10 love uhiuui07-June-2018hghju  banana"
matches = list(datefinder.find_dates(text))
if len(matches) > 0:   
# date returned will be a datetime.datetime object. here we are only using the first 
#match.
 print(matches)
else:
   print ('No dates found')
© www.soinside.com 2019 - 2024. All rights reserved.