Skip to content

@fatiherikli /tabularize.py
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Loading tabular data in Python
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 | edi@edi.com |
| budu | 123456 | budu@budu.com |
| budu | 123456 | budu@budu.com |
| budu | 123456 | budu@budu.com |
-------------------------------------------
""")
@fatiherikli
Owner

Example use case for testing

def test_foo():
    """
    ---------------------
    | n1  | n2 | result |
    ---------------------
    | 1     | 2   | 3   |
    | 4     | 5   | 9   |
    | 3     | 4   | 7   |
    ---------------------
    """
    for row in load(test_foo.__doc__):
        assert int(row["n1"]) + int(row["n2"]) == int(row["result"])

test_foo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.