Last active
August 1, 2016 10:10
-
-
Save Winand/3ac7ea98fdfdcc78392c6793d9d8b776 to your computer and use it in GitHub Desktop.
find column respecting row/colspans
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 0-based /row/ to search for /text/ | |
def find_column(table, row, text): | |
trs = table.find_all('tr') | |
results = [[c for c in trs[i].findChildren(recursive=False)] | |
for i in range(0, row+1)] | |
for r in range(row-1, -1, -1): | |
i = 0 | |
for c in results[r]: | |
if r+int(c.get('rowspan', 0)) > row: | |
results[-1].insert(i, None) | |
i += int(c.get('colspan', 1)) | |
i = 0 | |
for c in results[-1]: | |
if c and c.text.strip() == text: | |
return i | |
i += int(c.get('colspan', 1) if c else 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment