为了账号安全,请及时绑定邮箱和手机立即绑定

清理从扫描的 .pdf 中提取的文本数据

清理从扫描的 .pdf 中提取的文本数据

人到中年有点甜 2021-12-17 15:49:19
我正在创建一个脚本来从扫描的 pdf 中提取文本,以创建一个 JSON 字典,以便稍后在 MongoDB 中实现。我遇到的问题是,通过 Textract 模块使用 tesseract-ocr 成功提取了所有文本,但它正在被 python 读取,因此 PDF 上的所有空白都被转换为 '\n' 使得提取文本变得非常困难必要的信息。我试过用一堆代码来清理它,但它仍然不是很可读。它摆脱了所有冒号,我认为这将使识别键和值更容易。stringedText = str(text)cleanText = rmStop.replace('\n','')splitText = re.split(r'\W+', cleanText)caseingText = [word.lower() for word in splitText]cleanOne = [word for word in caseingText if word != 'n']dexStop = cleanOne.index("od260")dexStart = cleanOne.index("sheet")clean = cleanOne[dexStart + 1:dexStop]我仍然留下相当多的不干净几乎处理过的数据。所以在这一点上,我知道如何使用它。这就是我提取数据的方式text = textract.process(filename, method="tesseract", language="eng")我也试过 nltk,它取出了一些数据,让它更容易阅读,但仍有很多 \n 混淆了数据。这是 nltk 代码:stringedText = str(text)stop_words = set(stopwords.words('english'))tokens = word_tokenize(stringedText)rmStop = [i for i in tokens if not i in ENGLISH_STOP_WORDS]这是我从我尝试的第一次清理中得到的:['n21', 'feb', '2019', 'nsequence', 'lacz', 'rp', 'n5', 'gat', 'ctc', 'tac', 'cat', 'ggc', 'gca', 'cat', 'ttc', 'ccc', 'gaa', 'aag', 'tgc', '3', 'norder', 'no', '15775199', 'nref', 'no', '207335463', 'n25', 'nmole', 'dna', 'oligo', '36', 'bases', 'nproperties', 'amount', 'of', 'oligo', 'shipped', 'to', 'ntm', '50mm', 'nacl', '66', '8', 'xc2', 'xb0c', '11', '0', '32', '6', 'david', 'cook', 'ngc', 'content', '52', '8', 'd260', 'mmoles', 'kansas', 'state', 'university', 'biotechno', 'nmolecular', 'weight', '10', '965', '1', 'nnmoles']从那我需要一个 JSON 数组,它看起来像:"lacz-rp" : {  "Date" : "21-feb-2019",  "Sequence" : "gatctctaccatggcgcacatttccccgaaaagtgc"  "Order No." : "15775199"  "Ref No." : "207335463" }
查看完整描述

1 回答

?
守着一只汪

TA贡献1872条经验 获得超3个赞

您可以使用换行符转换您的 \n。请使用以下;

formatted_text = text.replace('\\n', '\n')

这将用输出中的实际换行符替换转义的换行符。


查看完整回答
反对 回复 2021-12-17
  • 1 回答
  • 0 关注
  • 159 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号