【发布时间】:2020-10-06 03:31:45
【问题描述】:
我有一个如下所示的输入文件,我正在尝试提取其文本并删除 html 标签。请注意,我希望每个 p 都在换行符中,但如果它是 br,我希望将其保留在同一行中,但无论如何都删除 br 标记。
<tt xmlns="http://www.w3.org/ns/ttml" xml:lang="en" xmlns:tts="http://www.w3.org/ns/ttml#parameter"><head><styling><style id="b1"/></sty ling></head><body><div xml:lang="en" style="b1"><p begin="" end="0.143">HISTORY</p><p begin="0.143" end="0.286">HISTORY TV"</p><p begin= "0.286" end="0.714">HISTORY TV" THIS</p><p begin="0.714" end="0.857">HISTORY TV" THIS<br/>WEEKEND</p><p begin="0.857" end="3">HISTORY TV " THIS<br/>WEEKEND ON</p><p begin="3" end="3.333">HISTORY TV" THIS<br/>WEEKEND ON C-SPAN3.</p><p begin="3.333" end="3.667">WEEKEND ON C- SPAN3.<br/>>>></p><p begin="3.667" end="4">WEEKEND ON C-SPAN3.<br/>>>> "THE</p><p begin="4" end="4.5">WEEKEND ON C-SPA N3.<br/>>>> "THE MARCH</p><p begin="4.5" end="5">WEEKEND ON C-SPAN3.<br/>>>> "THE MARCH ON</p><p begin="5" end="5.5">W EEKEND ON C-SPAN3.<br/>>>> "THE MARCH ON WASHINGTON"</p><p begin="5.5" end="5.667">>>> "THE MARCH ON WASHINGTON"<br/>F OR</p><p begin="5.667" end="5.833">>>> "THE MARCH ON WASHINGTON"<br/>FOR JOBS</p><p begin="5.833" end="6">>>> "THE MAR CH ON WASHINGTON"<br/>FOR JOBS AND</p><p begin="6" end="6.2">>>> "THE MARCH ON WASHINGTON"<br/>FOR JOBS AND FREEDOM</p><p begin ="6.2" end="6.4">>>> "THE MARCH ON WASHINGTON"<br/>FOR JOBS AND FREEDOM WAS</p><p begin="6.4" end="7">>>> "THE MARCH O N WASHINGTON"<br/>FOR JOBS AND FREEDOM WAS 49</p><p begin="7" end="8">FOR JOBS AND FREEDOM WAS 49<br/>YEARS</p><p begin="8" end="8.5">FO R JOBS AND FREEDOM WAS 49<br/>YEARS AGO.</p><p begin="8.5" end="8.75">YEARS AGO.<br/>ON</p><p begin="8.75" end="9">YEARS AGO.<br/>ON AUG UST</p><p begin="9" end="13">YEARS AGO.<br/>ON AUGUST 28th,</p><p begin="13" end="13.333">YEARS AGO.<br/>ON AUGUST 28th, 1963.</p><p beg in="13.333" end="13.5">ON AUGUST 28th, 1963.<br/>THE</p><p begin="13.5" end="13.667">ON AUGUST 28th, 1963.<br/>THE MARCH</p><p begin="13 .667" end="13.833">ON AUGUST 28th, 1963.<br/>THE MARCH WAS</p><p begin="13.833" end="14">ON AUGUST 28th, 1963.<br/>THE MARCH WAS ORKGANI ZED</p><p begin="14" end="14.167">ON AUGUST 28th, 1963.<br/>THE MARCH WAS ORKGANIZED TO</p><p begin="14.167" end="14.667">ON AUGUST 28th , 1963.<br/>THE MARCH WAS ORKGANIZED TO PUSH</p><p begin="14.667" end="14.833">THE MARCH WAS ORKGANIZED TO PUSH<br/>FOR</p>
所以最后我想拥有
HISTORY
HISTORY TV"
HISTORY TV" THIS
HISTORY TV" THIS WEEKEND
HISTORY TV" THIS WEEKEND ON
HISTORY TV" THIS WEEKEND ON C-SPAN3.
...etc
我如何完成这项任务?
我用过这段代码
import re
import os
def remove_html_tags(data):
p = re.compile(r'<.*?>')
return p.sub(' ', str(data)).strip()
directory = './reprocess'
for filename in os.listdir(directory):
if filename.endswith(".dfxp"):
print("Processing: {}".format(filename))
with open("./reprocess/"+filename, "r") as inputFile:
data = inputFile.read().splitlines()
new_data = ""
for line in data:
new_data = new_data + remove_html_tags(line) + "\n"
with open("./rmout/"+filename, "w") as text_file:
text_file.write(new_data)
但它给了我一个可怕的输出
HISTORY TV"
WEEKEND ON
HISTORY TV" THIS
"THE MARCH
"THE MARCH ON
WEEKEND ON C-SPAN3.
FOR JOBS AND FREEDOM WAS
"THE MARCH ON WASHINGTON"
YEARS
FOR JOBS AND FREEDOM WAS 49
ON AUGUST 28th,
YEARS AGO.
THE MARCH WAS ORKGANIZED TO
ON AUGUST 28th, 1963.
FOR COMPREHENSIVE CIVIL
THE MARCH WAS ORKGANIZED TO PUSH
INCLUDING PUBLIC
FOR COMPREHENSIVE CIVIL RIGHTS
DESEGREGATION,
DESEGREGATION, VOTING
【问题讨论】:
-
不要使用正则表达式解析 HTML 文件。使用
BeautifulSoup。 -
标签在 bs4 中可能是邪恶的,如 this answer 中的 cmets 所示。这种find_all-replace_with方法无法为我删除<br>标签(也许bs4 API 改变了?)。我想知道截至 2020 年是否存在干净可靠的解决方案(BeautifulSoup v4.9.1)。
标签: python python-3.x regex