diff --git a/HTML_Table_to_List/HTMLTableToList.py b/HTML_Table_to_List/HTMLTableToList.py new file mode 100644 index 0000000..0653c20 --- /dev/null +++ b/HTML_Table_to_List/HTMLTableToList.py @@ -0,0 +1,22 @@ +from bs4 import BeautifulSoup + +class HTMLTableToList(): + + def __init__(self, table_html): + self.table_html = table_html + + def get_list(self): + list_of_list = [] + soup = BeautifulSoup(self.table_html,"lxml") + table = soup.find('table') + all_tr = table.findAll('tr') + for tr in all_tr: + current_row = [] + all_th = tr.findAll('th') + all_td = tr.findAll('td') + for th in all_th: + current_row.append(th.text) + for td in all_td: + current_row.append(td.text) + list_of_list.append(current_row) + return list_of_list \ No newline at end of file diff --git a/HTML_Table_to_List/README.md b/HTML_Table_to_List/README.md new file mode 100644 index 0000000..c02627a --- /dev/null +++ b/HTML_Table_to_List/README.md @@ -0,0 +1,9 @@ +# HTML Table to Python List of List Converter +A simple tool which takes a HTML table as string, and converts it to python list of list data structure and returns the same. + +## Libraries Required +1. Beautiful Soap +`$pip install bs4` + +## Usage +A sample script `html_table_to_list_usage.py` has been provided to show the usage of the HTMLTableToList. It takes a string of html table, and prints the corresponding list of list. \ No newline at end of file diff --git a/HTML_Table_to_List/__init__.py b/HTML_Table_to_List/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/HTML_Table_to_List/html_table_to_list_usage.py b/HTML_Table_to_List/html_table_to_list_usage.py new file mode 100644 index 0000000..d7f1b7b --- /dev/null +++ b/HTML_Table_to_List/html_table_to_list_usage.py @@ -0,0 +1,35 @@ +from HTMLTableToList import HTMLTableToList +from pprint import pprint + +html_table_string = """
RGB | +53 | 72 | 35 | +
---|---|---|---|
HSL | 0.25 | 0.35 | 0.21 | +
HSV | 91° | 51° | 28° | +
CMYK | +0.26 | 0.00 | 0.51 0.72 | +
XYZ | 4.0889 | 5.5130 | 2.4387 | +
Yxy | 5.5130 | 0.3396 | 0.4579 | +
Hunter Lab | 23.4798 | -10.0046 | 10.2778 | +
CIE-Lab | 28.1490 | -15.1006 | 19.7427 | +