【问题标题】:How to convert appended list of strings into one single list如何将附加的字符串列表转换为一个列表
【发布时间】:2021-01-21 22:23:41
【问题描述】:

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

但是,我希望将其放在一个列表中。以下 sn-p 说明了我想要的输出。

下面是我写的简单代码:

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)

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

【问题讨论】:

标签: python beautifulsoup


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多