The reason is Because you are dict are unordered. You can use OrderedDict from the collections module.
>>> from collections import OrderedDict
>>> from tabulate import tabulate
>>> parameter_list = []
>>> parameter_list.append(OrderedDict([('C', False), ('A', 'Hello'), ('B', 'you')]))
>>> parameter_list.append(OrderedDict([('C', False), ('A', 'Salue'), ('B', 'Tu')]))
>>> print tabulate(parameter_list, headers='keys')
C A B
--- ----- ---
0 Hello you
0 Salue Tu
Answer from Sede on Stack OverflowHeader order
python - How to group columns alphabetically with tabulate module? - Stack Overflow
python - Printing Lists as Tabular Data - Stack Overflow
python - Setting the order of output for a table - Stack Overflow
» pip install tabulate
There are some light and useful python packages for this purpose:
1. tabulate
https://pypi.python.org/pypi/tabulate
from tabulate import tabulate
print(tabulate([['Alice', 24], ['Bob', 19]], headers=['Name', 'Age']))
Name Age
------ -----
Alice 24
Bob 19
tabulate has many options to specify headers and table format.
print(tabulate(
[['Alice', 24], ['Bob', 19]],
headers=['Name', 'Age'],
tablefmt='orgtbl'))
| Name | Age |
|--------+-------|
| Alice | 24 |
| Bob | 19 |
2. PrettyTable
https://pypi.python.org/pypi/PrettyTable
from prettytable import PrettyTable
t = PrettyTable(['Name', 'Age'])
t.add_row(['Alice', 24])
t.add_row(['Bob', 19])
print(t)
+-------+-----+
| Name | Age |
+-------+-----+
| Alice | 24 |
| Bob | 19 |
+-------+-----+
PrettyTable has options to read data from csv, html, sql database. Also you are able to select subset of data, sort table and change table styles.
3. texttable
https://pypi.python.org/pypi/texttable
from texttable import Texttable
t = Texttable()
t.add_rows([['Name', 'Age'], ['Alice', 24], ['Bob', 19]])
print(t.draw())
+-------+-----+
| Name | Age |
+=======+=====+
| Alice | 24 |
+-------+-----+
| Bob | 19 |
+-------+-----+
with texttable you can control horizontal/vertical align, border style and data types.
4. termtables
https://github.com/nschloe/termtables
import termtables as tt
string = tt.to_string(
[["Alice", 24], ["Bob", 19]],
header=["Name", "Age"],
style=tt.styles.ascii_thin_double,
# alignment="ll",
# padding=(0, 1),
)
print(string)
+-------+-----+
| Name | Age |
+=======+=====+
| Alice | 24 |
+-------+-----+
| Bob | 19 |
+-------+-----+
with texttable you can control horizontal/vertical align, border style and data types.
Other options
- terminaltables - Easily draw tables in terminal/console applications from a list of lists of strings. Supports multi-line rows.
- asciitable can read and write a wide range of ASCII table formats via built-in Extension Reader Classes.
Some ad-hoc code:
row_format ="{:>15}" * (len(teams_list) + 1)
print(row_format.format("", *teams_list))
for team, row in zip(teams_list, data):
print(row_format.format(team, *row))
This relies on str.format() and the Format Specification Mini-Language.
Your problem is that you expect:
>>> {'a': 1, 'b': 2}.items()
[('a', 1), ('b', 2)]
When in fact, sometimes you get
[('b', 2), ('a', 1)]
Why not just construct this data structure directly?
first_table = [
("Total agents" , len(agentUserIdDict)),
("Total users" , len(pure_user_dict)),
("Users with agent" , len(pure_users_with_agents_dict)),
("Users with invites" , len(user_id_invite_dict)),
("RUURL accounts" , len(ruurl_set))
]
f.write(tabulate(first_table, headers = ["Category", "Total"]))
from collections import OrderedDict
from tabulate import tabulate
b=OrderedDict()
b["Total agents"] = 45571
b["Total users"] = 90818
b["Users with agent"] = 70060
b["Users with invites"] = 81533
b["RUURL accounts"] = 81288
print tabulate(b.items(),headers = ["Category", "Total"])
Category Total
------------------ -------
Total agents 45571
Total users 90818
Users with agent 70060
Users with invites 81533
RUURL accounts 81288