I am using the Tabulate 0.7.7 package to tabulate a dictionary with two values per key.
I have this code.
from tabulate import tabulate
d = {"Dave":("13", "Male") , "Sarah":("16", "Female")}
headers = ["Name", "Age", "Gender"]
print(tabulate(d.items(), headers = headers))
I want the following table to be produced -
Name Age Gender
------ ----- ---------
Dave 13 Male
Sarah 16 Female
However, the code is the following table -
Name Age
------ ----------------
Dave ('13', 'Male')
Sarah ('16', 'Female')
How can I solve this problem?
If you flatten the dictionary items, from (k, (v1, v2)) to (k, v1, v2), you can get the correct format:
from tabulate import tabulate
d = {"Dave":("13", "Male") , "Sarah":("16", "Female")}
headers = ["Name", "Age", "Gender"]
print(tabulate([(k,) + v for k,v in d.items()], headers = headers))
Name Age Gender
------ ----- --------
Sarah 16 Female
Dave 13 Male
I'm not sure that it is possible given the structure of your input data d.
Reformatting your data as such (see below) will give the desired result.
d = {'Name': ['Dave', 'Sarah'], 'Age': [13, 16], 'Gender': ['Male', 'Female']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With