如何将附加的字符串列表转换为一个列表

时间:2021-01-21 22:23:41

标签: python beautifulsoup

我有一个 HTML 文件,我对带有文本的 BBox 信息感兴趣。用文本提取 BBox 后,我将其附加到列表中。但是,输出似乎将第一个列表(首先将第一行添加到列表中)附加到第二个列表中(将第二行字符串添加到列表中)。为了更好地说明这个问题,我附上了这个问题的一个片段。 enter image description here

但是,我希望将其合并到一个列表中。以下代码段说明了我想要的输出。 enter image description here

下面是我写的简单代码:

import bs4

xml_input = open("1.html","r",encoding="utf-8")
soup = bs4.BeautifulSoup(xml_input,'lxml')
ocr_lines = soup.findAll("span", {"class": "ocr_line"})
#We will save coordinates of line and the text contained in the line in lines_structure list
lines_structure = []
for line in ocr_lines:
    line_text = line.text.replace("\n"," ").strip()
    title = line['title']
    #The coordinates of the bounding box
    x1,y1,x2,y2 = map(int, title[5:title.find(";")].split())
    lines_structure.append({"x1":x1,"y1":y1,"x2":x2,"y2":y2,"text": line_text})
    print(lines_structure)

非常感谢您对此问题的帮助。

1 个答案:

答案 0 :(得分:1)

实际上,经过挖掘,我发现打印需要在'for'循环之外。这是一个快速修复。感谢您抽出宝贵时间。

相关问题