python3提取txt文件中两个字符串之间的字符串

时间:2017-07-22 20:31:02

标签: python html regex python-3.x parsing

我是Python新手。我试图从一个txt文件(“infile.txt”)中提取一个字符串(“我们的披露控件的结论是有效的”)。该文件相对较大,我需要在一个特定部分中查找上述字符串(在“ITEM& nbsp; 9A”和“ITEM& nbsp; 9B”之间)。以下是一个例子:

</A>ITEM&nbsp;9A. CONTROLS AND PROCEDURES. </B></FONT></P> <P STYLE="margin-top:6px;margin-bottom:0px"><FONT STYLE="font-family:Times New Roman" SIZE="2"><B>Evaluation of Disclosure Controls and Procedures </B></FONT> STYLE="margin-top:6px;margin-bottom:0px; text-indent:4%"><FONT STYLE="font-family:Times New Roman" SIZE="2">Under the supervision and with the participation of our management, including our Chief Executive Officer and Chief Financial Officer, we conducted an evaluation of the effectiveness of our disclosure controls and procedures (as defined in Rules 13a-15(e) and 15d-15(e) under the Securities Exchange Act of 1934, as amended (Exchange Act)), as of the end of the period covered by this Annual Report on Form 10-K. Management recognizes that any controls and procedures, no matter how well designed and operated, can provide only reasonable assurance of achieving their objectives and management necessarily applies its judgment in evaluating the cost-benefit relationship of possible controls and procedures. Based on such evaluation, our Chief Executive Officer and Chief Financial Officer concluded that our disclosure controls and procedures were effective as of September&nbsp;28, 2012. </FONT></P> <P STYLE="margin-top:18px;margin-bottom:0px"><FONT STYLE="font-family:Times New Roman" SIZE="2"><B>Management&#146;s Annual Report on Internal Control over Financial Reporting </B></FONT> <P STYLE="margin-top:6px;margin-bottom:0px; text-indent:4%"><FONT STYLE="font-family:Times New Roman" SIZE="2">This Annual Report does not include a report of management&#146;s assessment regarding internal control over financial reporting or an attestation report of the company&#146;s registered public accounting firm due to a transition period established by rules of the Securities and Exchange Commission for newly public companies. </FONT> <P STYLE="margin-top:18px;margin-bottom:0px"><FONT STYLE="font-family:Times New Roman" SIZE="2"><B>Changes in Internal Control over Financial Reporting </B></FONT></P> <P STYLE="margin-top:6px;margin-bottom:0px; text-indent:4%"><FONT STYLE="font-family:Times New Roman" SIZE="2">There were no changes in our internal control over financial reporting (as defined in Rule&nbsp;13a-15(f) under the Exchange Act) during the quarter ended September&nbsp;28, 2012, that have materially affected, or are reasonably likely to materially affect, our internal control over financial reporting. </FONT> <P STYLE="margin-top:18px;margin-bottom:0px"><FONT STYLE="font-family:Times New Roman" SIZE="2"><B><A NAME="tx431171_16"></A>ITEM&nbsp;9B. OTHER INFORMATION.

如果该部分具有所需的字符串“得出结论我们的披露控制措施是有效的”(上面的章节中间有aprox。),那么我想在单独的“输出”中打印“1”。 csv“文件,如果没有,则打印”未找到“。该部分的起点并不总是与一条线的起点重合。我很抱歉,但无法弄清楚如何开始......我正在使用Python 3.6。

非常感谢你!

2 个答案:

答案 0 :(得分:0)

您可以使用regular expressions在给定的开场白和近距离之间提取文字:

import re

opener = re.escape(r"ITEM&nbsp;9A")
closer = re.escape(r"ITEM&nbsp;9B")

您可以使用re.finditer查看数据提取,然后使用in-operator过滤目标字符串的数据:

target_string = "concluded that our disclosure controls were effective as of"
for mo in re.finditer(opener + '(.*?)' + closer, inputstring, re.DOTALL):
    extract = mo.group(1)
    if target_string in extract:
        ...

希望这足以让你开始: - )

答案 1 :(得分:0)

您可以使用re.findall

import re

the_data = re.findall("</A>ITEM&nbsp;9A. (.*?)</B>", string_data_from_file)

if len(the_data) >0:
    print "1"

else:
    print "Not found"