I have a dictionary:
d = {1: ["Spices", math.floor(random.gauss(40, 5))],
2: ["Other stuff", math.floor(random.gauss(20, 5))],
3: ["Tea", math.floor(random.gauss(50, 5))],
10: ["Contraband", math.floor(random.gauss(1000, 5))],
5: ["Fruit", math.floor(random.gauss(10, 5))],
6: ["Textiles", math.floor(random.gauss(40, 5))]
}
I want to print it out so it lines up nicely with headers. Can I add the headers to the dictionary and always be sure they come out on top? I've seen a few ways to do it vertically but I'd like to have it come out with max column widths close to the max str() or int().
Example:
Key________Label__________Number
1__________Spices_________42
2__________Other Stuff______16
etc
Apparently I can't even do this inside of this editor manually, but I hope the idea comes across.
I also don't really want the __ either. Just a place holder.
Thanks all.
You can use string formatting:
Output:
I would prefer pandas DataFrame
Output:
check out more about printing pretty a dataframe here
You can use ljust or rjust string methods:
I was looking for a solution with unknown columns width to print a database table. So here it is:
Sample:
Output:
In Psycopg context, you can use it this way:
If you need a variant for multi-lines rows, here it is:
Sample:
Output:
Without
sep
parameter,printTable(sampleDict)
gives you:Based on Le Droid's code, I added separator '-' for each row which could make the print more clear. Thanks, Le Droid.
Output: