Skip to content

Instantly share code, notes, and snippets.

@originalankur
Forked from fatiherikli/tabularize.py
Created January 29, 2013 09:13
Show Gist options
  • Save originalankur/4662918 to your computer and use it in GitHub Desktop.
Save originalankur/4662918 to your computer and use it in GitHub Desktop.
def normalize_line(line):
return [piece.strip() for piece in line.split("|")[1:-1]]
def is_valid_line(line):
return "|" in line
def load(text):
lines = map(normalize_line,
filter(is_valid_line,
text.strip().splitlines()))
keys = lines.pop(0)
return [dict(zip(keys, line)) for line in lines]
print load("""
-------------------------------------------
| username | password | email |
-------------------------------------------
| edi | 123456 | |
| budu | 123456 | [email protected] |
| budu | 123456 | [email protected] |
| budu | 123456 | [email protected] |
| budu | 123456 | [email protected] |
-------------------------------------------
""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment