Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I pass a list to headers? #2

Open
qm64 opened this issue Jan 22, 2024 · 1 comment
Open

Can I pass a list to headers? #2

qm64 opened this issue Jan 22, 2024 · 1 comment

Comments

@qm64
Copy link

qm64 commented Jan 22, 2024

Summary: I want to pass a list of dicts as the data, and a list of keys for headers. I don't want to full list of keys in the data as headers, I want a shorter list, in a different order.

In the documentation:

The following tabular data types are supported:
...
dict of iterables (keys as columns)
...

The headers parameter requires either a special keyword (e.g., "keys"), or a dict of column keys. I already have a list of column keys. (In a bizarre complication, the code converts this dict of column keys to a list? But doesn't support a list of column keys directly? WTF?) [I agree, it's nice that the header dict is converted to a list for me.)

My header list is not always the full list of column keys, as the full data is collected first, then a decision is made on which columns to output.

A workaround is to convert my list of column keys to a dict, so the code can convert it back?

@qm64
Copy link
Author

qm64 commented Jan 22, 2024

Hmmm...further complications...

headers=some_dict, but the keys are not limited to the keys or values in some_dict, they are in fact ALL of the keys present in the data! (Again, WTF?)

In this scenario, headers only provides a mapping from actual key names to output column names. If there is no matching key in the headers dict, the original key from the original data is used. ¯\(ツ)

            elif isinstance(headers, dict):
                # a dict of headers for a list of dicts
                headers = [headers.get(k, k) for k in keys]
                headers = list(map(_text_type, headers))

So I also need to filter out key/value pairs that I don't want in the output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant