【问题标题】:Remove html and uuencode from .txt file从 .txt 文件中删除 html 和 uuencode
【发布时间】:2017-04-14 22:54:36
【问题描述】:

我想处理一个包含大量 html 和 uuencode 字符的文本文件:

例如,请参阅以下链接中的 .txt 文件:

https://www.sec.gov/Archives/edgar/data/1522690/000121390016011794/0001213900-16-011794.txt

我正在使用以下代码:

从 bs4 导入 BeautifulSoup

def strip_non_ascii(string):
    ''' Returns the string without non ASCII characters'''
    stripped = (c for c in string if 0 < ord(c) < 127)
    return ''.join(stripped)

with open("C:/EDGAR/forms_to_process/10K/20160322_10-K_edgar_data_1522690_0001213900-16-011794_1.txt") as f:
    lines = f.readlines()
    with open("PROCESSED.txt", 'w', encoding='utf-8') as f1:
        i=1
        for line in lines:
            soup = BeautifulSoup(line, "lxml")
            print(i, "Initial line: ", line)
            print(i, "Soup get text line: ", soup.get_text())
            bs_line = soup.get_text()
            ascii_line = strip_non_ascii(bs_line)
            print(i, "Ascii line: ", ascii_line)
            f1.write(ascii_line)
            i=i+1


f.close()
f1.close();

这将文件从 8.5 MB 减少到 2.5 MB,但它仍然有很多我不需要的元素,例如:

</tr>
<tr style="vertical-align: bottom; background-color: #cceeff;">
<td
style="padding: 0px 0px 0px 10pt; text-indent: -10pt;"><font style="font-family: 'times new roman', times, serif;">&#160;</font></td>
<td><font style="font-family: 'times new roman', times, serif;">&#160;</font></td>
<td style="text-align: left;"><font style="font-family: 'times new roman', times, serif;">&#160;</font></td>
<td style="text-align: right;"><font style="font-family: 'times new roman', times, serif;">&#160;</font></td>
<td style="text-align: left;"><font style="font-family: 'times new roman', times, serif;">&#160;</font></td>
<td><font style="font-family: 'times new roman', times, serif;">&#160;</font></td>
<td style="text-align: left;"><font style="font-family: 'times new roman', times, serif;">&#160;</font></td>
<td style="text-align: right;"><font style="font-family: 'times new roman', times, serif;">&#160;</font></td>
<td style="text-align: left;"><font style="font-family: 'times new roman', times, serif;">&#160;</font></td>

EXCEL
86
Financial_Report.xlsx
IDEA: XBRL DOCUMENT

begin 644 Financial_Report.xlsx
M4$L#!!0    (  J%=D@6'2-4(0(  $8I   3    6T-O;G1E;G1?5'EP97-=
M+GAM;,W:2V[;,! &X*L8VA86S62DZ(U
MW")I8^#?6):'G!EII&_EJV\/@=+BX(8QK:LNY_"!L=1TY&RJ?:"Q1#8^.IO+
M:=RR8)N=W1(3JY5AC1\SC7F9IQS5]=67/<78M[3X> Q,N=>5#6'H&YM[/[+]
MV)YD7?K-IF^H]M31U1=D.=\L- Z5S]8^2I\@UM[-V07U3X\=[5D89Y3>KZ\%CJTZ%D2>6W=56B
MZ5D53C?^K;/>34,+X_:W'=/Y/U[+R4WM[KY[OWO-QX2FJVJI7898%L;M([5?MWH+T\0ZDC_M D56@2*K0)%5H,@J4&05*+(*%%D%BJP215:)(JM$D56BR"I19)4HLDH4626*
MK!)%5HDBJT*15:'(JE!D52BR*A19%8JL"D56A2*K0I%5HMBJP:15:-(JM&D56CR*I19-4HLAH460V*K 9%5H,BJT&1U:#(:E!D-2BR&A19

有没有办法删除这些并只保留文本文件中包含的相关文本信息?

编辑:从我提供的链接中,我想保留的文本示例是:

<P STYLE="font: 10pt/normal Times New Roman,serif; margin: 0; text-align: justify">The table above indicates the current yields
to maturity (YTM) for the senior bonds of selected life insurance carriers with durations, on average, that our similar to our
life insurance portfolio.&nbsp; The average yield to maturity of these bonds was 3.02% which, we believe, reflects in part the
financial market&rsquo;s judgement that credit risk is low with regard to these carriers&rsquo; financial obligations. It should
be noted that the obligations of life insurance carriers to pay life insurance policy benefits is senior in rank to any other obligation.&nbsp;
This &ldquo;super senior&rdquo; priority is not reflected in the yield to maturity in the table and, if considered, would result
in a lower yield to maturity all else being equal. As such, as long as the respective premium payments have been made, it is highly
likely that the owner of the insurance policy will collect the insurance policy benefit upon the mortality of the insured.</P>

即我想删除所有的 html 标签和 uuencoding 二进制文件,只保留文本。

编辑 2:

Gerrit 下面的回答肯定非常接近我想要达到的效果,至少对于正在考虑的 .txt 文件。但是,它仍然在文件末尾留下以下部分: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K*K4
M\.?\AR#Z-_(UE7_AR-B23R64-RK3D%]RNW0D\9Z=33+?1-'MM9?5HK>X%TQ9B2C[
M06ZD#'^/\ 9%.TO6[IY[:QDMY9B$03W&[.
M&9-^>@&.0*J\M7W8NRTI8_P"BQ(%V,N\*#N'S @L,@_A4
M=QX@U18YW2TAAV6;3[)F.X,'*YZ=.,]J/>[A[BOH6;K1-)NWN#(EZ([EM\T2
M&14=O[Q4=^E33Z=837+W*-?V\LBA9&MS;(GF <#=CJ??K5.;Q#=V=U/$8/M,K
M2X2)"<*!$C, 0.>3QFGR^)+M))!'IZLB-(H+SX/[M0S9&..#^='O#O THH;.
M&^>\1+CSWB6%BR.:J0Z1I4.MR:NL5R;Q\Y9E<@9&#

这似乎是 uuencoding 二进制部分。知道如何摆脱这种情况吗?

【问题讨论】:

  • BeautifulSoup 的理念是只过滤那些你需要的元素。例如:for div in soup.findAll(["td"]) 让汤来干活。但如果文件大小有问题,您可以在处理文件之前转换文件。在转换过程中,您可以过滤掉不需要的元素
  • 抱歉,您能说得更准确一点吗?您能否编写一些代码,让我摆脱 html 和 td 以及所有标签,例如 .xlsx uuencode blob 代码,同时保留文本?
  • 您能否提供您想要保留的文本示例? (包括它周围的html)。它是否包含在特定标签中,例如

    或特定部分中,例如用 id 或类标识?

  • sample if text in

    : soup = BeautifulSoup(open("C:/EDGAR/forms_to_process/10K/20160322_10-K_edgar_data_1522690_0001213900-16-011794_1.txt"),"html5lib") for p in soup.findAll(["p"]): labelName = div.get_text() Soup解析html,这样你就可以用html结构找到你需要的元素了

  • 嗨 Gerrit,我编辑了我最初的帖子并举了一个例子。希望它有所帮助,我正在寻找一种方法来从文件中删除 HTML、ASCII 编码的二进制文件和任何其他不打算分析的嵌入式文档结构。顺便说一句,所有 .txt 文件都可以在我最初帖子的链接中找到。谢谢。

标签: python html text beautifulsoup uuencode


【解决方案1】:

在审查 SEC 文件时,必须记住它由标题数据和一个或多个文件组成。文件可以是多种类型,如 HTML、PDF、TXT、JPG、GIF、ZIP 等。

因为 JPG 和 GIF 文件类型通常具有“不可打印”字符,所以它们是 uuencoded,并且必须进行解码,以便文件返回到“正确”状态以供正常使用。

对于您的示例归档,归档详细信息页面 (https://www.sec.gov/Archives/edgar/data/1522690/000121390016011794/0001213900-16-011794-index.html) 显示有 8 个 HTML 页面、两个图形 (jpg)、XML 和 XSD 文件。如果您需要使用作为完整提交的 RAW "Accession-Number.txt" 文件,您必须解析出各个文件并执行 uudecode 作为该过程的一部分。

【讨论】:

  • 我该如何使用这是问题的代码来做到这一点。
【解决方案2】:

我会使用汤来选择您真正需要的文本,而不是过滤掉不需要的文本。如果此文本包含在 &lt;p&gt; 标记中,则:

from bs4 import BeautifulSoup
from bs4 import SoupStrainer

only_p_tags = SoupStrainer("p")

soup = BeautifulSoup(open("C:/EDGAR/forms_to_process/10K/20160322_1‌​0-K_edgar_data_15226‌​90_0001213900-16-011‌​794_1.txt"), "html.parser", parse_only=only_p_tags)

for p in soup:
    print p.get_text()

【讨论】:

  • 嗨格瑞特。请参阅我原始帖子中的编辑。我赞成你的回答,因为它肯定会产生接近我想要的东西,但它仍然会在最后留下二进制 uuencoding 部分。请看我编辑的帖子。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-21
  • 1970-01-01
  • 2011-07-19
  • 2017-02-07
相关资源
最近更新 更多