python - output dictionary data as a table in console -


i output dict data in form of table in console:

dtc={ "test_case1_short":{"test_step11":"pass","test_step12":"pass","test_step_13":{"status":"failed","details":"ca marche po"}, "test_case2_longest_name":{"test_step21":"ne","test_step22":"ne"}, "test_case3_medium_name":{"test_step31":"ne","test_step32":"ne"} }

note: french speakers 'dtc' shortcut dict_test_collection (!)

to build table determine size of key names in order dimension column headers. can key names max length doing this:

max = 0 in list(dtc.keys()): if max < len(i):     max = len(i) print(max) 

but find not straighforward ... there way information dict.keys() or dict feature ?

besides, i'd set separators "+-----------------------------+" section headers , "| |" section bodies, have looking table. in section bodies, there straight , simple way set table , columns width (ie. '|' caracter ending @ column 50, whatever text on line, padding line spaces until column)

thank

alexandre

is there way information dict.keys() or dict feature ?

this seems straightforward me:

max_key_len = max(len(key) key in dtc) print(max_key_len) 

this 1 seems less straightforward, shorter:

max_key_len = max(map(len, dtc)) print(max_key_len) 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -