Skip to content

Instantly share code, notes, and snippets.

@twpayne
Created September 27, 2010 13:24
Show Gist options
  • Save twpayne/599019 to your computer and use it in GitHub Desktop.
Save twpayne/599019 to your computer and use it in GitHub Desktop.
A parser for tables in columns
#!/usr/bin/python
import re
class ColumnParser(object):
def __init__(self, header):
self.columns = {}
index = 0
for m in re.finditer(r' (?=\S)', header):
self.columns[header[index:m.start()].rstrip()] = (index, m.start())
index = m.start() + 1
self.columns[header[index:].rstrip()] = (index, None)
def parse(self, line):
result = {}
for key, (start, end) in self.columns.items():
result[key] = line[start:end].rstrip()
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment