python - 如何使用python提取文本文件中的特定段落?
问题描述
我必须提取从“替代受托人”开始并以“根据上述信托契约”结尾的特定段落。
由于字段重复需要仅在段落内查找数据。
数据可能像日期、文档编号等
sample.txt
Inst #: 2021
Fees: $42.00
06/24/2021 06:54:48 AM
Receipt #: 4587188
Requestor:
FINANCIAL CORPORATION OF
After recording return to: Src: MAIL
Mail Tax Statements to:
SUBSTITUTION OF TRUSTEE
AND DEED OF RECONVEYANCE
The undersigned, Financial Corporation of Nevada, a Nevada Corporation, as the Owner and
Holder of the Note secured by Deed of Trust dated March 1, 2013 made by Elvia Bello, Trustor, to
Official Records -- HEREBY substitutes Financial Corporation of Nevada, a Nevada Corporation,
as Trustee in lieu of the Trustee therein.
Said Note, together with all other indebtedness secured by said Deed of Trust, has been fully paid
satisfied; and as successor Trustee, the undersigned does hereby RECONVEY WITHOUT
WARRANTY TO THE PERSON OR PERSONS LEGALLY ENTITLED THERETO, all the estate now
held by it under said Deed of Trust.
This JNO aay of June 2021,
Financial Corporation
wy luo Rtn rae
import re
mylines = []
pattern = re.compile(r"SUBSTITUTION OF TRUSTEE", re.IGNORECASE)
with open(r'sample.txt', 'rt', encoding='utf-8') as myfile:
for line in myfile:
mylines.append(line)
for line in mylines:
if(line == "SUBSTITUTION OF TRUSTEE "):
print(line)
break
else:
mylines.remove(line)
print("my lines",mylines)
解决方案
substitution of trustee
您可以首先在子字符串的开头检查每一行,一旦找到,将标志变量设置为True。当标志为真时,继续向mylines
列表中添加行。然后,一旦到达包含 的行under said deed or trust
,停止添加行并返回结果:
mylines = []
flag = False
with open(r'sample.txt', 'rt', encoding='utf-8') as myfile:
for line in myfile:
if line.strip().upper().startswith("SUBSTITUTION OF TRUSTEE"):
flag = not flag
if flag:
mylines.append(line)
if "under said deed of trust" in line.strip().lower():
break
print("".join(mylines))
请参阅此 Python 演示。
输出:
SUBSTITUTION OF TRUSTEE
AND DEED OF RECONVEYANCE
The undersigned, Financial Corporation of Nevada, a Nevada Corporation, as the Owner and
Holder of the Note secured by Deed of Trust dated March 1, 2013 made by Elvia Bello, Trustor, to
Official Records -- HEREBY substitutes Financial Corporation of Nevada, a Nevada Corporation,
as Trustee in lieu of the Trustee therein.
Said Note, together with all other indebtedness secured by said Deed of Trust, has been fully paid
satisfied; and as successor Trustee, the undersigned does hereby RECONVEY WITHOUT
WARRANTY TO THE PERSON OR PERSONS LEGALLY ENTITLED THERETO, all the estate now
held by it under said Deed of Trust.
推荐阅读
- json - 在 Typescript 中导入 Json 文件:终端出错
- reactjs - 在 React 中初始加载后停止重新加载选项卡
- postgresql - postgresql上的不一致记录数据
- python - 使用 tkinter 在单个窗口中将多个框架与小部件组合在一起
- node.js - Jade(PUG) 到 Handlebars,用于加载 SVG 图像的辅助功能问题
- php - Laravel (5.8) php 单元发出原始发布请求
- spring - 出现错误:java.lang.IllegalStateException:找不到配置服务器(配置服务器)的实例
- typescript - 如何为测试编写文档注释并使用 typedoc 制作报告?
- sql - 如何组合来自两个 SQL 查询的结果?
- jquery - 如何在没有提交按钮操作的情况下以jsp形式上传文件