Skip to content

Instantly share code, notes, and snippets.

@rpmuller
Created November 19, 2015 20:22
Show Gist options
  • Save rpmuller/2cb56d87a7306835e4cf to your computer and use it in GitHub Desktop.
Save rpmuller/2cb56d87a7306835e4cf to your computer and use it in GitHub Desktop.
Parse an orgmode table into a Pandas dataframe
def parse_orgtable(s):
"""Parse an org-table (input as a multiline string) into a Pandas data frame."""
# This didn't quite work, because spaces messed things up
#import cStringIO
#table = pd.read_table(cStringIO.StringIO(s),sep='|',skiprows=[1],skipinitialspace=True)
#table = table.drop(table.columns[[0,-1]],1)
# I suppose I could have simply run strip() on all of the resulting column names, since that was the problem
def parseline(l):
w = l.split('|')[1:-1]
return [wi.strip() for wi in w]
lines = s.splitlines()
columns = parseline(lines[0])
data = []
for line in lines[2:]:
data.append(map(float,parseline(line)))
return pd.DataFrame(data=data,columns=columns)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment