Last active
February 8, 2017 20:08
-
-
Save Taywee/1ca37222f71ab7e072b18a37a60be0b6 to your computer and use it in GitHub Desktop.
A very simple python function that will split apart a textual table into rows and columns (use six to replace range and zip if you need good performance in python 2). Public Domain (CC0)
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
# To the extent possible under law, Taylor C. Richberger has waived all copyright and related or neighboring rights to splittable. This work is published from: United States. | |
def splittable(table): | |
'''An incredibly simple routine to split a textual table based on "gutters" | |
in it. Only works on a line-by-line basis, so any tables with multi-line | |
rows will not work. It also needs a solid verticle whitespace gutter | |
between lines. Does not work with tabs. If you have a table with tabs, it | |
will need to be preexpanded before being passed in. Does not strip, so if | |
you need cells to be stripped, it will have to be done after this | |
function. The columns can only span to the shortest line, so empty columns | |
on the right side of the table will need to be filled with spaces to work properly.''' | |
lines = table.strip('\n').splitlines() | |
shortestline = min(len(line) for line in lines) | |
gutters = set() | |
for i in range(shortestline): | |
# The character is a separator | |
if all(line[i].isspace() for line in lines): | |
gutters.add(i) | |
deletegutters = set() | |
# If multicharacter gutters exist, we only want the rightmost | |
for gutter in gutters: | |
if gutter + 1 in gutters: | |
deletegutters.add(gutter) | |
gutters -= deletegutters | |
gutters = sorted(list(gutters)) | |
output = [[line[i + 1:j] for i, j in | |
zip([-1] + gutters, gutters + [None])] for line in lines] | |
return output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment