How to parse a table with multiple headings?

There is a HTML table

<table border="1" width="100%" bordercolor="#AAAAAA" bgcolor="#F2F2F2" cellspacing="0" cellpadding="1" style="border-collapse:collapse"><tbody><tr></tr></tbody></table><table width="90%"align="center" border="1" bordercolor="#AAAAAA" bgcolor="#F2F2F2" cellspacing="0" cellpadding="1" style="border-collapse:collapse">
<tbody>
<td align="center" bgcolor="#565A5C"><font face="Arial, Helvetica, sans-serif" color="#ffffff" size="2"><b>
Pod 0</b></font></td>
<td align="center" width="20%" bgcolor="#565A5C"><font face="Arial, Helvetica, sans-serif" color="#ffffff" size="2"><b>Response Score</b></font></td>
<td align="center" width="20%" bgcolor="#565A5C"><font face="Arial, Helvetica, sans-serif" color="#ffffff" size="2"><b>Retries</b></font></td>
<td align="center" width="20%" bgcolor="#565A5C"><font face="Arial, Helvetica, sans-serif" color="#ffffff" size="2"><b>Clear Retries</b></font></td>
</tr>
the <tr>
<td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>Disk 1</b></font></td>
<td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>2.21</b></font></td><td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>0</b></font></td><td align="center"><input type="checkbox" name="clr0" value="1"></td>
</tr>
the <tr>
<td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>Disk 2</b></font></td>
<td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>2.01</b></font></td><td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>0</b></font></td><td align="center"><input type="checkbox" name="clr1" value="1"></td>
</tr>

<td align="center" bgcolor="#565A5C"><font face="Arial, Helvetica, sans-serif" color="#ffffff" size="2"><b>
Pod 1</b></font></td>
<td align="center" width="20%" bgcolor="#565A5C"><font face="Arial, Helvetica, sans-serif" color="#ffffff" size="2"><b>Response Score</b></font></td>
<td align="center" width="20%" bgcolor="#565A5C"><font face="Arial, Helvetica, sans-serif" color="#ffffff" size="2"><b>Retries</b></font></td>
<td align="center" width="20%" bgcolor="#565A5C"><font face="Arial, Helvetica, sans-serif" color="#ffffff" size="2"><b>Clear Retries</b></font></td>
</tr>
the <tr>
<td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>Disk 1</b></font></td>
<td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>1.89</b></font></td><td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>0</b></font></td><td align="center"><input type="checkbox" name="clr16" value="1"></td>
</tr>
the <tr>
<td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>Disk 2</b></font></td>
<td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>1.00</b></font></td><td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>0</b></font></td><td align="center"><input type="checkbox" name="clr17" value="1"></td>
</tr>

<td align="center" bgcolor="#565A5C"><font face="Arial, Helvetica, sans-serif" color="#ffffff" size="2"><b>
Pod 2</b></font></td>
<td align="center" width="20%" bgcolor="#565A5C"><font face="Arial, Helvetica, sans-serif" color="#ffffff" size="2"><b>Response Score</b></font></td>
<td align="center" width="20%" bgcolor="#565A5C"><font face="Arial, Helvetica, sans-serif" color="#ffffff" size="2"><b>Retries</b></font></td>
<td align="center" width="20%" bgcolor="#565A5C"><font face="Arial, Helvetica, sans-serif" color="#ffffff" size="2"><b>Clear Retries</b></font></td>
</tr>
the <tr>
<td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>Disk 1</b></font></td>
<td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>2.08</b></font></td><td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>0</b></font></td><td align="center"><input type="checkbox" name="clr32" value="1"></td>
</tr>
the <tr>
<td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>Disk 2</b></font></td>
<td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>2.15</b></font></td><td align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#000000"><b>0</b></font></td><td align="center"><input type="checkbox" name="clr33" value="1"></td>
</tr>
</td>
</tr>
</tbody></table><br>


I use BeautifulSoup and exported to JSON get
[{'Disk 1': 2.21}, {'Disk 2': 2.01}, {'Disk 3': 2.08}, {'Disk 4': 2.15}, {'Disk 5': 2.27}, {'Disk 6': 2.07}, {'Disk 7': 1.98} ...


How do I add to Disk x the name of the title to the conclusion
[{'Pod1-Disk 1': 2.21}, {'Pod1-Disk 2': 2.01}, ...{'Pod2-Disk 1': 2.21}, {'Pod2-Disk 2': 2.01},


My code
def parse(html):
 table_data = []
 soup = BeautifulSoup(html, 'lxml')
 table = soup.find(
 'table', attrs={"border": "1", "width": "90%"})
 rows = table.find_all('tr')[2:]

 for row in rows:
 cols = row.find_all('td')
 cols = [ele.text.strip() for ele in cols]
 table_data.append([ele for ele in cols if ele])
 final_data = []

 for data in table_data:
 d = dict([(k, v) for k, v in zip(data[::2], data[1::2])])
final_data.append(d)
 formated_json = remove_quotes(json.dumps(final_data))
March 20th 20 at 11:35
1 answer
March 20th 20 at 11:37
So stopping to check contains in a 'Pod' or not? If it is, then the variable stavitsya write the whole text from this cell. Continue pass and if you stumble again on the 'Pod' will be replaced with your variable.

Find more questions by tags PythonParsing