Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将附加的字符串列表转换为单个列表_Python_Beautifulsoup - Fatal编程技术网

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

Python 如何将附加的字符串列表转换为单个列表,python,beautifulsoup,Python,Beautifulsoup,我有一个HTML文件,我对BBox信息和文本感兴趣。在用文本提取BBox之后,我将其附加到一个列表中。但是,输出似乎是将第一个列表(首先将第一行添加到列表中)追加到第二个列表(将第二行字符串添加到列表中)。为了更好地说明这个问题,我附上了这个问题的一个片段。 但是,我想把它放在一个列表中。下面的代码片段演示了我想要的输出。 下面是我编写的简单代码: import bs4 xml_input = open("1.html","r",encoding=&

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

但是,我想把它放在一个列表中。下面的代码片段演示了我想要的输出。

下面是我编写的简单代码:

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)

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

事实上,在挖掘之后,我发现打印需要在“for”循环之外。这是一个快速修复。谢谢您的时间。

请以文本形式提供数据。您是否正在尝试展平列表?[1,2],[3],[4,5,6],[7,8]->[1,2,3,4,5,6,7,8]使用扩展而不是追加。更多信息: