在智能完钻井大数据处理过程中,先期要从多格式非结构化的文献中获取信息,为后期的数据挖掘、处理做准备。由于很多文献都是caj或者pdf格式文档,需要将这些文档转成可编辑的word或者是txt文档,然后才能做数据的提取。然而很多早期的pdf文档为扫描版的,噪点很多。此外caj文档也需要转成pdf,再转成word或者txt格式。在caj或者pdf格式转换到word文档时,在word文档中经常会产生乱码字符。为了后期分类处理中文、数字和特殊字符,需要将中文、数字和特殊字符提取出来,文本规范化模块就是完成该任务。
开发环境:Pycharm2019+Anconda3
编程语言:Python3.7
使用到的库:docx、re
解析要求:
1、纯汉字的文本
2、汉字加数字的文本
3、纯数字文本
一、正则表达式
1、提取纯汉字文本
正则表达式:[\u4e00-\u9fa5\。\、\,\?\:\”\“]+
2、提取汉字加数字文本
正则表达式:[\u4e00-\u9fa5\。\,\?\:\”\“\d+\.?\d*]+"
3、代码片段
# 读取word文档并提取
docStr = docx.Document(docx_name)
for paragraph in docStr.paragraphs:
parstr = paragraph.text
#pattern="[\u4e00-\u9fa5]+"
#regex = re.compile(pattern)
#parstr_filter=regex.findall(parstr)
# 只保留汉字
# ch_chara_filter=re.sub("[\@\/\#\$\%\&\[\]\d+\.?\d*]+","",parstr)
ch_chara_filter=re.findall("[\u4e00-\u9fa5\。\、\,\?\:\”\“]+",parstr)
cont_ch_chara.extend(ch_chara_filter)
# cont_ch_chara.append(ch_chara_filter)
# cont_ch_chara.append('\n')
# 保留汉字和数字
# ch_chara_numb_filter=re.sub("[\*\@\/\#\$\%\&\[\]]+","",parstr)
ch_chara_numb_filter=re.findall("[\u4e00-\u9fa5\。\,\?\:\”\“\d+\.?\d*]+",parstr)
cont_ch_chara_numb.extend(ch_chara_numb_filter)
# 提取的汉字和汉字数字内容写txt文档
# 创建txt文件,并写入处理后的文本
post_txtfile=open('posttxt.txt','w')
# 提取汉字文本写txt文件
post_txtfile.write(cont_ch_chara_str)
post_txtfile.write('\n')
# 提取汉字+数字文本写txt文件
post_txtfile.write(cont_ch_chara_numb_str)
post_txtfile.write('\n')
作者:LiPF

在智能完钻井大数据处理中,需从caj或pdf文献转成word/txt进行信息提取。面对乱码问题,通过Python3的docx和re库实现文本规范化,提取纯汉字、汉字加数字及纯数字文本。

2215

被折叠的 条评论
为什么被折叠?



