# -*- encoding: utf-8 -*- from bs4 import BeautifulSoup html = """
| ID | Name |
|---|---|
| 56 | Matt |
| 42 | Colin |
| 23 | Lisa |
| 45 | John |
<table> |
<e> |
'
for i, row in enumerate(rows):
for j, cell in enumerate(row):
text += '| ' + ''.join(str(node) for node in cell.contents) + ' ' * (cols_width[j] - len(cell.text))
text += '|\n'
text += ''
return text
def pre_tables(html):
soup = BeautifulSoup(html, 'html.parser')
for table in soup.find_all('table'):
table.replace_with(BeautifulSoup(pre_table(table), 'html.parser'))
return str(soup)
if __name__ == "__main__":
# CSW: ignore
print(pre_tables(html))