Created
September 27, 2010 13:24
-
-
Save twpayne/599019 to your computer and use it in GitHub Desktop.
A parser for tables in columns
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
#!/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