How to use tabulate library?

Question:

I am trying to use tabulate with the zip_longest function. So I have it like this:

from __future__ import print_function
from tabulate import tabulate
from itertools import zip_longest
import itertools
import locale
import operator
import re


50 ="['INGBNL2A, VAT number: NL851703884B01 inTel, +31 (0}1 80 61 88 nnrut ard wegetablesnx0c']"

fruit_words = ['Appels', 'Ananas', 'Peen Waspeen',
               'Tomaten Cherry', 'Sinaasappels',
               'Watermeloenen', 'Rettich', 'Peren', 'Peen', 'Mandarijnen', 'Meloenen', 'Grapefruit']

def total_amount_fruit_regex(format_=re.escape):
    return r"(d*(?:.d+)*)s*~?=?s*(" + '|'.join(
        format_(word) for word in fruit_words) + ')'

def total_fruit_per_sort():
    number_found = re.findall(total_amount_fruit_regex(), verdi50)

    fruit_dict = {}
    for n, f in number_found:
        fruit_dict[f] = fruit_dict.get(f, 0) + int(n)
        result = 'n'.join(f'{key}: {val}' for key, val in fruit_dict.items())   
    return result


def fruit_list(format_=re.escape):
        return "|".join(format_(word) for word in fruit_words)


def findallfruit(regex):
    return re.findall(regex, verdi50)


def verdi_total_number_fruit_regex():
    return rf"(d*(?:.d+)*)s*W+(?:{fruit_list()})"


def show_extracted_data_from_file():
    regexes = [
       verdi_total_number_fruit_regex(),
    ]
    matches = [findallfruit(regex) for regex in regexes]
    fruit_list = total_fruit_per_sort().split("n")
    return "n".join(" t ".join(items) for items in zip_longest(tabulate(*matches, fruit_list, headers=['header','header2'], fillvalue='', )))
    

print(show_extracted_data_from_file())

But then I get this error:

TypeError at /controlepunt140
tabulate() got multiple values for argument 'headers'

So how to improve this?

So if you remove the tabulate function. Then the format looks like this:

16   Watermeloenen: 466
360      Appels: 688
6    Sinaasappels: 803
75   
9    
688      
22   
80   
160      
320      
160      
61   

So expected output is with headers:

header1  header2
-------  -------
16       Watermeloenen: 466
360      Appels: 688
6        Sinaasappels: 803
75   
9    
688      
22   
80   
160      
320      
160      
61

Like how it works in tabulate.

Asked By: mightycode Newton

||

Answers:

You should be passing a single table to the tabulate() function, passing multiple lists results in the TypeError: tabulate() got multiple values for argument 'headers' you are seeing.

Updating your return statement –

def show_extracted_data_from_file():
    regexes = [
       verdi_total_number_fruit_regex(),
    ]
    matches = [findallfruit(regex) for regex in regexes]
    fruit_list = total_fruit_per_sort().split("n")
    return tabulate(zip_longest(*matches, fruit_list), headers=['header1','header2'])

Output:

  header1  header2
---------  ------------------
       16  Watermeloenen: 466
      360  Appels: 688
        6  Sinaasappels: 803
       75
        9
      688
       22
       80
      160
      320
      160
       61
Answered By: Jay
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.