Skip to content

Instantly share code, notes, and snippets.

@rybak
Created September 26, 2018 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rybak/4f4f3dbbe67a1e99758d0e12abc999c1 to your computer and use it in GitHub Desktop.
Save rybak/4f4f3dbbe67a1e99758d0e12abc999c1 to your computer and use it in GitHub Desktop.
Script to flip HTML-like tables upside down
#
# 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