jazzband / tablib

Python Module for Tabular Datasets in XLS, CSV, JSON, YAML, &c.
https://tablib.readthedocs.io/
MIT License
4.59k stars 590 forks source link

WIP: preliminary support for XML, 2020 #464

Open DeepSpace2 opened 4 years ago

DeepSpace2 commented 4 years ago

Hi all.

Before I start, I'm aware of #224 but it is 4 years old and tablib has changed quite extensively since then (dropped Python 2 support, format extensions are now classes, etc) so I thought I'd have a go at it.

It's a WIP and only supports export of dataset, but it supports tags.

The dataset

data = Dataset(headers=['first_name', 'last_name', 'gpa'])
data.append(('John', 'Adams', 90))
data.append(('George', 'Washington', 67), tags=['tag 1', 'tag 2'])

is exported as

<dataset>
    <row>
        <first_name>John</first_name>
        <last_name>Adams</last_name>
        <gpa>90</gpa>
    </row>
    <row tags="tag 1,tag 2">
        <first_name>George</first_name>
        <last_name>Washington</last_name>
        <gpa>67</gpa>
    </row>
</dataset>

I do have a question: Is there a proper way to access a row's tags while iterating over the dataset? The only way I found is to use enumerate and then use the row's index to index into _data which of course if unfavorable.

for row_index, row in enumerate(dataset.dict):
    tags = dataset._data[row_index].tags