Python Forum
format the output from a nested dictionary.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
format the output from a nested dictionary.
#3
If you want formatting, I prefer using a formatting package. tabulate has worked well for me.

from tabulate import tabulate

currentuser ={'root':{'uid': 0,'username': 'root' ,'shell': '/bin/bash'},
        'daemon': {'uid': 1,'username': 'daemon' ,'shell': '/usr/sbin/nologin'},
        'bin': {'uid': 2,'username': 'bin', 'shell':'/usr/sbin/nologin'},
        'nsa': {'uid': 1000,'username': 'nsa', 'shell': '/bin/bash'}}


columns = list(next(iter(currentuser.values())).keys())
print(tabulate([x.values() for x in currentuser.values()], headers=columns, tablefmt="presto"))
Output:
uid | username | shell -------+------------+------------------- 0 | root | /bin/bash 1 | daemon | /usr/sbin/nologin 2 | bin | /usr/sbin/nologin 1000 | nsa | /bin/bash
ndc85430 likes this post
Reply


Messages In This Thread
RE: format the output from a nested dictionary. - by bowlofred - May-03-2021, 01:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  need to compare 2 values in a nested dictionary jss 2 1,718 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Nested dictionary acting strange Pedroski55 2 2,965 May-13-2021, 10:37 PM
Last Post: Pedroski55
Lightbulb Python Nested Dictionary michaelserra 2 3,751 Apr-18-2021, 07:54 AM
Last Post: michaelserra
  Can I format decimal places by column with a dictionary? Mark17 2 3,786 Dec-28-2020, 10:13 PM
Last Post: Mark17
  JupyterLab Dictionary Content Output Format Ourkid123uk 0 1,739 Sep-04-2020, 02:18 PM
Last Post: Ourkid123uk
  Issue accessing data from Dictionary/List in the right format LuisSatch 2 3,017 Jul-25-2020, 06:12 AM
Last Post: LuisSatch
  nested dictionary rkpython 7 4,490 May-29-2020, 11:13 AM
Last Post: rkpython
  Nested Dictionary/List tonybrown3 5 4,262 May-08-2020, 01:27 AM
Last Post: tonybrown3
  Format SQL Query Output phillyfa 2 5,878 Apr-22-2020, 07:45 AM
Last Post: buran
  Save output into a Excel Sheet with Format Table skaailet 1 3,164 Apr-17-2020, 11:56 PM
Last Post: thirteendec

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020