pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/noises1990/Awesome-Python-Scripts/commit/80c46d29d2e83a17fcd986034e71e678b834b2b4

igin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/global-94620c216484da1f.css" /> Added HTML_Table_to_List · noises1990/Awesome-Python-Scripts@80c46d2 · GitHub
Skip to content

Commit 80c46d2

Browse files
committed
Added HTML_Table_to_List
1 parent 1ebff83 commit 80c46d2

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from bs4 import BeautifulSoup
2+
3+
class HTMLTableToList():
4+
5+
def __init__(self, table_html):
6+
self.table_html = table_html
7+
8+
def get_list(self):
9+
list_of_list = []
10+
soup = BeautifulSoup(self.table_html,"lxml")
11+
table = soup.find('table')
12+
all_tr = table.findAll('tr')
13+
for tr in all_tr:
14+
current_row = []
15+
all_th = tr.findAll('th')
16+
all_td = tr.findAll('td')
17+
for th in all_th:
18+
current_row.append(th.text)
19+
for td in all_td:
20+
current_row.append(td.text)
21+
list_of_list.append(current_row)
22+
return list_of_list

HTML_Table_to_List/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# HTML Table to Python List of List Converter
2+
A simple tool which takes a HTML table as string, and converts it to python list of list data structure and returns the same.
3+
4+
## Libraries Required
5+
1. Beautiful Soap
6+
`$pip install bs4`
7+
8+
## Usage
9+
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.

HTML_Table_to_List/__init__.py

Whitespace-only changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from HTMLTableToList import HTMLTableToList
2+
from pprint import pprint
3+
4+
html_table_string = """<table class="table table-condensed">
5+
<tr>
6+
<th>RGB</th>
7+
<td>53</td><td>72</td><td>35</td>
8+
</tr>
9+
<tr>
10+
<th>HSL</th><td>0.25</td><td>0.35</td><td>0.21</td>
11+
</tr>
12+
<tr>
13+
<th>HSV</th><td>91&deg;</td><td>51&deg;</td><td>28&deg;</td>
14+
</tr>
15+
<tr>
16+
<th>CMYK</th>
17+
<td>0.26</td><td>0.00</td><td>0.51 &nbsp; 0.72</td>
18+
</tr>
19+
<tr>
20+
<th>XYZ</th><td>4.0889</td><td>5.5130</td><td>2.4387</td>
21+
</tr>
22+
<tr>
23+
<th>Yxy</th><td>5.5130</td><td>0.3396</td><td>0.4579</td>
24+
</tr>
25+
<tr>
26+
<th>Hunter Lab</th><td>23.4798</td><td>-10.0046</td><td>10.2778</td>
27+
</tr>
28+
<tr>
29+
<th>CIE-Lab</th><td>28.1490</td><td>-15.1006</td><td>19.7427</td>
30+
</tr>
31+
</table>"""
32+
33+
htmltabletolist = HTMLTableToList(html_table_string) ## args : HTML table as string
34+
list_of_list = htmltabletolist.get_list()
35+
pprint(list_of_list)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy