Skip to content

Improve error reporting when tabular_data is a dict of non-iterable values #124

Closed

Description

@WolfgangFahl

I am trying to tabulate a counter from collections counter

print(tabulate(counter))

this approach gives an error message:

 File "/Users/wf/Library/Python/3.8/lib/python/site-packages/tabulate.py", line 1528, in tabulate
    list_of_lists, headers = _normalize_tabular_data(
  File "/Users/wf/Library/Python/3.8/lib/python/site-packages/tabulate.py", line 1089, in _normalize_tabular_data
    izip_longest(*tabular_data.values())
TypeError: 'int' object is not iterable

What else should i do - or would this be a feature request?

Activity

  1. WolfgangFahl commented on May 3, 2021

    @WolfgangFahl
    Author

    This is what it did so far to get my result - unfortunately floatfmt doesn't seem to work:

    def showCounter(self,counter,title,headers=["#","key","count","%"],tablefmt='pretty',limit=50):
            '''
            show the counter contents
            '''
            bins=len(counter.keys())
            limit=min(bins,limit)
            total=sum(counter.values())
            table=[("total",bins,total)]
            for i,bintuple in enumerate(counter.most_common(limit)):
                key,count=bintuple
                table.append((i+1,key,count,count/total*100.0))
            print(title)
            print(tabulate(table,headers=headers,tablefmt=tablefmt,floatfmt=".2f"))
  2. ivan-radic commented on May 17, 2021

    @ivan-radic

    Hi,

    I'd like to second this feature request.
    It would be great to have built-in support for collections Counters, instead of writing my own wrapper function.

    Best regards,
    Ivan

  3. patrickdeanbrown commented on Nov 4, 2021

    @patrickdeanbrown

    Hi @WolfgangFahl,

    When tablefmt='pretty' is set, tabulate appears to be treating each column as if it were a string, prior to applying floatfmt. So no float formatting is being applied, even though the floatfmt parameter is set. As a workaround, you can either not specify tablefmt, or you can apply the format builtin when calculating your data.

    table.append((i + 1, key, count, format(count / total * 100.0, "0.2f")))

    As for directly using a Counter object with tabulate, you can call the Counter.items() method, and it should work.

    from collections import Counter
    counter = Counter(['a', 'a', 'a', 'b', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'c', ])
    print(tabulate(counter.items()))
    
    # Expected output:
    #
    # -  -
    # a  3
    # b  2
    # c  7
    # -  -
  4. WolfgangFahl commented on Nov 5, 2021

    @WolfgangFahl
    Author

    @patrickdeanbrown thank you for looking into this. Your workarounds are nice but do not solve the problem/fulfill the need of a simpler interface that this issue is requesting.

  5. astanin commented on Oct 19, 2022

    @astanin
    Owner

    Tabulate can process an iterable (list) of dictionaries. It should work with a list of Counters too:

    >>> c = Counter(a = 3, b = 4)
    >>> print(tabulate([dict(c)], headers="keys"))
      a    b
    ---  ---
      3    4
    >>> print(tabulate([c], headers="keys"))
      a    b
    ---  ---
      3    4

    Using counter.items() is the intended way to print similar data as rows.

    >>> print(tabulate(c.items()))
    -  -
    a  3
    b  4
    -  -
  6. astanin commented on Oct 19, 2022

    @astanin
    Owner

    But the error message can be improved. The library should have suggested what data shape it expects.

  7. changed the title [-]tabulate collections counter[/-] [+]Improve error reporting when tabular_data is a dict of non-iterable values[/+] on Oct 19, 2022
  8. added this to the v0.9.1 milestone on Oct 19, 2022
  9. added a commit that references this issue on Dec 28, 2022
    96761ee
  10. added a commit that references this issue on Sep 26, 2024
    f660f66
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions