0

I have dict like below. i want to print the below data as a table in command line

Input:

{
    'PERFORMANCE_SCHEMA_DIGESTS_SIZE': {'To': '-1', 'From': '10000'},
    'PERFORMANCE_SCHEMA_SESSION_CONNECT_ATTRS_SIZE': {'To': '-1', 'From': '512'},
    'ENCRYPT_TMP_DISK_TABLES': {'To': 'OFF', 'From': 'ON'},
    'INNODB_READ_IO_THREADS': {'To': '4', 'From': '10'}
}

Output:

-------------------------------------------------------------------------------
| Perameter                        | Previous Value(From) | currunt value(To) |
-------------------------------------------------------------------------------
| PERFORMANCE_SCHEMA_DIGESTS_SIZE  | 10000                | -1                |
-------------------------------------------------------------------------------

Can someone help me with this? I have tried with pandas but my dict format not supporting

Thanks in advance.

match
  • 10,388
  • 3
  • 23
  • 41
  • using pandas, turn your dict into a dataframe using `df = pd.DataFrame.from_dict()` and then you can just print 'print(df)' – Bijay Regmi Dec 01 '21 at 10:02
  • Does this answer your question? [Print a dictionary into a table](https://stackoverflow.com/questions/29265002/print-a-dictionary-into-a-table) – match Dec 01 '21 at 10:06

1 Answers1

0

You can do something like this:

def dict_to_table(input_dict: dict):

    number_of_columns = 3
    column_width = len(max(input_dict.keys())) + 1

    line_sep = '-' * column_width * number_of_columns + '-' * 4

    print(line_sep)

    # Header
    print(f"|{'Parameter':^{column_width}s}"
          f"|{'Previous Value(From)':^{column_width}s}"
          f"|{'Current value(To)':^{column_width}s}|")

    # Content
    for item in input_dict:
        column_1_text = item
        column_2_text = input_dict[item]["From"]
        column_3_text = input_dict[item]["To"]

        print(line_sep)

        print(f"|{column_1_text:{column_width}s}"
              f"|{column_2_text:^{column_width}s}"
              f"|{column_3_text:^{column_width}s}|")

    print(line_sep)

For your input data, above function will print:

----------------------------------------------------------------------------------------------------------------------------------------------
|                  Parameter                   |             Previous Value(From)             |              Current value(To)               |
----------------------------------------------------------------------------------------------------------------------------------------------
|PERFORMANCE_SCHEMA_DIGESTS_SIZE               |                    10000                     |                      -1                      |
----------------------------------------------------------------------------------------------------------------------------------------------
|PERFORMANCE_SCHEMA_SESSION_CONNECT_ATTRS_SIZE |                     512                      |                      -1                      |
----------------------------------------------------------------------------------------------------------------------------------------------
|ENCRYPT_TMP_DISK_TABLES                       |                      ON                      |                     OFF                      |
----------------------------------------------------------------------------------------------------------------------------------------------
|INNODB_READ_IO_THREADS                        |                      10                      |                      4                       |
----------------------------------------------------------------------------------------------------------------------------------------------
Dan Constantinescu
  • 1,426
  • 1
  • 7
  • 11