重新发布这个问题并附上具体细节(因为最后一个问题已被标记下来)。
我正在努力从档案卡中解析混乱的 (tesseract-ocr),以获取至少 50% 的信息 (date1)。数据行包含不同形式的日期,如下面的数据示例所示。
Raw_Text
1 "15957-8 . 3n v g - vw, 1 ekresta . bowker, william e tley n0 .qu v- l. c.
s. peteris, forestville, n. y. .mafae date1 june 17,1942 by davis, c. j6
l. g. b. jonnis, buffalo, n. y. ngsted decl 17, 1949.3y 7 davis, c. j.
date3 by j date4 - by date5 by 6 -.5/, 7/19/l date6 17 jul 1916 salamanca.
hf date7 31 dec 1986 buffalo, new york "
2 ".1o2o83n5ddn.. -i ekresta i bowles, albert edwin i made date1 june 9p1909
by parker, elm. date2 dec . 18 w date3 . by dep osed by date5 by date7mqm
9 ivvld wm 4144, mac, .75 076 eaqlwli "
3 "i naime bowles, charles edward made date1 may 31. 1892 by mclaren, wneoi
date2 may 18. 1895 by mclaren, w.e. date3 . i by date4 may 10. 1908 by
bip. of chicago. date5 by date7 "
4 "101 557 am l i ekrestaibowles, donald manson ..46 ohio trlnlty cathedral,
cleveland, ohio made date1 6/19/76 by burt, ji. h. grace , cleveland, ohio
date2 11 jun 77 by bp j h burt date3 . 1 .. by date4 by date5 bv m cuyahoga
heights, ohio date6 4/29/27 date7 240000 "
5 "227354 101 575 m68, frederick augustus st. paujjs cathedral, buffalo,
n.y. made date1 6/15/63 by scaife. l.i... st. thomas. modia, bath, n.y.
date2 1/11/611 by scaife. l.eo date3 by date4 by date5 by bradford, n.y. i
. 130m 6/1/18 date7 17 jun 1996 foratvme new york z4uc-xl "
6 "1 95812d ll. il ekresta bowles, harry oscar lmade date14 july 17, 190433,
lepnard, w.a. date2 july 25 , 1905 by leonard, w.a. i date3 by date4 by
date5 by g- m. /(,,/mr date7 jay /z/,. /357i l /mwi yk/maj. "
7 "5025 ,.. 2.57631 il . - . .. .1 i ekresta bowles , jedwiah hibbafd made
deac0n 8., i5-0i1862i13y potter, iih. date2 10. 280 1864 1 biy stevens, w.
b. date3 by date4 7 .30 l 1875 by date5 by date7 "
8 "30.611126 ekhq il ekresta bowles, ralph hart made date1 12. 210 i1883 by
iwiiiliams, i36 date2 7.. 1. 1885 by williams , j. date3 by i date4 by
date5 by g .97) l/am 9- date7 10. 4. 1900 (78) if x/ma 3.4, 154.47.11.73.
4,... mya-ix "
9 "2.25678 . 1o14593 ekresta bowles, robert brigham, jr. st. matthew s
cathedra1,da11quexas made date1 6/18/65 by mason, c. a. 57 mmzws camp
dr7///9s tams date2 12 21 cs by 14.45.42 c a date3 i by date4 by date5 ,
by houston, texas date6 4/11/30 date7 12 dec 2000 dallas texas 2400-xi "
10 "101 619 34hq woe ekresta bowlin1 howard bruce cathedral modia of saint
peter 61 st. paul, washin ton, dc made date1 13 jun 92 bybp r h haines
(wdc st. alban1s modia, annandale, vir inia . pdumd 16 jan 93 by r h halnes
(wdc) date3 by atas by date4 v by date5 by date6 31 aug 1946 e st. louis.
il date7 2400-i "
11 "w k8 8km tm boiling jack dnnmwm q- f grace ch , made dat j 11201). salem
mares. stverrett. f. ,w a x st. johms modia. memphis, tenh. date1 apr. 25.
1955 - bv barth, t.in.. date3 4 by date4 by date5 by date7 wq iw r 1 w .n
. 4.1- 1 date6z1l7i1c. "
我通过两步过程解析 date1,
- 1. 解析名称“date1”和“by”之间的文本
- 2.使用日期解析器提取实际日期
import re
import dateutil.parser as dparser
for lines in Raw_Text:
lines = lines.lower() #make lower case
lines = lines.strip() #remove leading and ending spaces
lines = " ".join(lines.split()) #remove duplicated spaces
# Step 1
#Extract data between "date1" and "by"
deacondt = re.findall(r'date1(.*?)by',lines)
deacondt = ''.join(deacondt) #Convert list to a string
# Step 2
# use dateutil to parse dates in extracted data
try:
deacondt1 = dparser.parse(deacondt)
except:
deacondt1 = 'NA'
print deacondt1
步骤 1 的输出为,
[' june 17,1942 ']
[' june 9p1909 ']
[' may 31. 1892 ']
[' 6/19/76 ']
[' 6/15/63 ']
['4 july 17, 190433, lepnard, w.a. date2 july 25 , 1905 ']
[]
[' 12. 210 i1883 ']
[' 6/18/65 ']
[' 13 jun 92 ']
[]
而步骤 2 返回以下输出
2018-06-17 00:00:00
1909-06-17 21:00:00
1892-05-31 00:00:00
1976-06-19 00:00:00
2063-06-15 00:00:00
NA
NA
NA
2065-06-18 00:00:00
1992-06-13 00:00:00
NA
第 2 步未能给出所有日期。 Python 2.7 是否有比“dateutil.parser”更好的日期解析器?