Skip to main content

Tables

parse_html_table(...)​

( html_table, include_header=False, remove_empty_columns=False )​

Parses an html-table to a list of dictionaries (per row) that contain cell-data. Expects column-keys in the classname of each cell.

Parameters:html_table (str)
String of a valid HTML-table.
include_header (bool)
Whether to skip the first row (header) of the table.
remove_empty_columns (bool)
Whether to omit cell-data of columns where all cells have no content-value.
Returns:Celldata per column, per row.
Return Type:list[dict[dict[str,str]]]
HTML-table
<table>
<tr>
<td class="column_key_1" attr="val">Size X</td>
<td class="column_key_2" attr="val">Size Y</td>
</tr>
<tr>
<td class="column_key_1" attr="val2">Size X2</td>
<td class="column_key_2" attr="val2">Size Y2</td>
</tr>
</table>
Result
[
{
"column_key_1": {
"row_index":0,
"column_index":0,
"column_key":"column_key_1",
"value":"Size X",
"is_column_header":True,
"attr":"val",
},
"column_key_2": {
"row_index":0,
"column_index":1,
"column_key":"column_key_2",
"value":"Size Y",
"is_column_header":True,
"attr":"val",
}
},
{
"column_key_1": {
"row_index":1,
"column_index":0,
"column_key":"column_key_1",
"value":"Size X2",
"is_column_header":False,
"attr":"val2",
},
"column_key_2": {
"row_index":1,
"column_index":1,
"column_key":"column_key_2",
"value":"Size Y2",
"is_column_header":False,
"attr":"val2",
}
}
]

get_row_values(...)​

( rows, include_header=False )​

Retrieve data from an iterated ElementTree of an HTML- / XML-table and enrich it with row- and column-indexes to a tabledata structure.

Parameters:rows (list)
A list of xml-Elements created by reading an HTML-table using ElementTree like: list(iter(ElementTree.XML(html_table)))
include_header (bool)
Whether to skip the first row (header) of the table.
Returns:Tabledata of an HTML-table with celldata in a structure per row, per column.
Return Type:list[dict[str,dict]]

remove_empty_columns_from_table(...)​

( table_data, include_header=False ):​

Iterate over tabledata from get_row_values to remove celldata of columns, where no cell has a value.

Parameters:_table_data (list[dict[str,dict]])
include_header (bool)
Whether to skip the first row (header) of the table.
Returns:Tabledata with celldata in a structure per row, per column.
Return Type:list[dict[str,dict]]

table_data_to_html(...)​

( data, include_header=True, first_row_is_header=True, include_attributes:bool=False )​

Create an HTML-string from a previously parsed table.

Parameters:_data (list[dict[str,dict]])
Tabledata resulted from a table previously parsed by function parse_html_table.
include_header (bool)
Whether to skip the first row (header) of the table.
first_row_is_header (bool)
Whether the first row is a header-row.
include_attributes (bool)
Whether the add attributes of celldata as attributes on the cell's HTML-node.
Returns:A HTML-table as string.
Return Type:str