0

我有一个 .htm 文件。通过使用 text_content(),我从文档中提取了文本。以下是正文:

'第 II 部分 \xa0\r\n公司\x92s 普通股在场外交易市场交易,并在纳斯达克全球精选市场以代码\r\nAAPL 和在法兰克福证券交易所以 APCD 代码进行报价. 普通股的价格范围 下面列出的普通股每股价格范围代表公司\x92s 普通股\r\n 在最近两年的每个季度中在纳斯达克全球精选市场之外的最高和最低销售价格。\xa0\r\n持有人 截至 2009 年 10 月\xa016,共有 30,573 名登记在册的股东。股息\r\n 公司在 2009 年或 2008 年均未宣布或支付现金股息。公司预计在可预见的\r\n未来将保留任何收益以用于其业务运营。发行人及其附属机构购买的股本证券\r\n购买者无。\xa0\r\n 33 '

使用此文本,我需要删除前面和后面有一个空行的标题。因此,应删除以下形式的行:

\n
some text here\n
\n

我有一个为 .txt 版本的文档执行此操作的代码。但是,从 .htm 文档中,我意识到像 \xa0\r\n 这样的一些奇怪的东西被用来使单词大写(例如)。有没有办法删除所有这些东西并正确删除标题?

这是删除标题的函数:

def clean_text_passage(a_text_string):
    """REMOVE /n:  take a list of strings (some passage of text)
and remove noise which is defined as lines that are preceded
by a blank line and followed by a blank line that is lines of
this form will not be in the output
\n
some text here\n
\n
"""
    new_passage=[]
    p=[line+'\n' for line in a_text_string.split('\n')]
    passage = [w.lower().replace('</b>\n', '\n') for w in p]

    if len(passage[0].strip())>0:
       if len(passage[1].strip())>0:
           new_passage.append(passage[0])

    for counter, text_line in enumerate(passage[:-1]):
        len_line_before=len(passage[counter-1].strip())
        len_line_after=len(passage[counter+1].strip())
        if len_line_before==len_line_after==0:
            continue
        if len(text_line.strip())!=0:
            new_passage.append(text_line)

    if len(passage[-2].strip())!=0:
        if len(passage[-1].strip())!=0:
            new_passage.append(passage[-1])

    return new_passage

我想关键是要识别 htm 文档中的标题。

非常感谢您的时间和帮助。

4

0 回答 0