Created
September 26, 2018 12:23
-
-
Save rybak/4f4f3dbbe67a1e99758d0e12abc999c1 to your computer and use it in GitHub Desktop.
Script to flip HTML-like tables upside down
This file contains 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
# | |
# Helper script to flip HTML-like tables with tbody tags upside down | |
# | |
# MIT License | |
# Copyright (c) 2018 Andrei Rybak | |
# | |
def pretty_print_tr(a_tr): | |
return ''.join(a_tr) | |
with open('input.txt') as f: | |
lines = f.readlines() | |
table_started = False | |
table_ended = False | |
pre_table = [] | |
post_table = [] | |
table = [] | |
one_tr = [] | |
for line in lines: | |
if '<tbody' in line: | |
table_started = True | |
pre_table.append(line) | |
continue | |
if '</tbody' in line: | |
table_ended = True | |
post_table.append(line) | |
continue | |
if not table_started: | |
pre_table.append(line) | |
continue | |
if table_ended: | |
post_table.append(line) | |
continue | |
one_tr.append(line) | |
if '</tr' in line: | |
table.append(one_tr) | |
one_tr = [] | |
table = list(map(pretty_print_tr, reversed(table))) | |
with open('output.txt', 'w') as f: | |
f.write(''.join(pre_table)) | |
f.write(''.join(table)) | |
f.write(''.join(post_table)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment