Reading tabular data with tabularize.py
I decided to make gherkin-like tabular data parser after using the lettuce. Using the texts as test data might be clear and understandable for reading or maintaining test cases.
Here is the example tabular data:
--------------------------------------
| city | country | continent |
---------------------------------------
| Istanbul | Turkey | Asia |
| Ankara | Turkey | Asia |
| Dusseldorf | Germany | Europe |
---------------------------------------
Now let's parse this data with tabularize.py
import tabularize
cities = tabularize.loads("""
This is a comment
--------------------------------------
| city | country | continent |
---------------------------------------
| Istanbul | Turkey | Asia |
| Ankara | Turkey | Asia |
| Dusseldorf | Germany | Europe |
---------------------------------------
Tabularize.py reads lines are starts with pipe (|)
""")
# the value of cities:
# [
# {
# "city": "Istanbul",
# "continent": "Asia",
# "country": "Turkey"
# },
# {
# "city": "Ankara",
# "continent": "Asia",
# "country": "Turkey"
# },
# {
# "city": "Dusseldorf",
# "continent": "Europe",
# "country": "Germany"
# }
# ]
The module consist with load, loads and from_string methods. As can be
understood with method names imitates the json and yaml like libraries.
You can parse any string with loads method. Likewise the load method allows
parse any file-like object that contains read method. And also there's a method
that called from_docstring to the read docstrings of objects.
You can specify the return type of items with the return_type parameter of
load and loads methods.
cities = tabularize.loads("""
--------------------------------------
| city | country | continent |
---------------------------------------
| Istanbul | Turkey | Asia |
| Ankara | Turkey | Asia |
| Dusseldorf | Germany | Europe |
---------------------------------------
""", return_type=tuple)
for city, country, continent in cities:
print city, country, continent
Source code available at github. You can look into the tests to see examples.