Posts: 9
Threads: 3
Joined: May 2021
May-02-2021, 10:37 PM
(This post was last modified: May-03-2021, 01:22 AM by Yoriz.
Edit Reason: Added code tags
)
Hello everyone im trying to make my codes output to look like this output,
without any trailing or leading symbols. Any tips on how i can do that?
The expected output should look like this.
Output: | 0 | root | /bin/bash
| 1 | bin | /usr/bin/nologin
| 2 | daemon | /usr/bin/nologin
| 1000 | nsa | /bin/bash
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'}}
for key, value in currentuser.items():
print(f"\nKey: {key}")
print(f"Value: {value}")
Posts: 1,144
Threads: 114
Joined: Sep 2019
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'}}
for v in currentuser.values():
print(f'|{v["uid"]}|{v["username"]}|{v["shell"]}') Output: |0|root|/bin/bash
|1|daemon|/usr/sbin/nologin
|2|bin|/usr/sbin/nologin
|1000|nsa|/bin/bash
Posts: 1,583
Threads: 3
Joined: Mar 2020
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
Posts: 9
Threads: 3
Joined: May 2021
(May-02-2021, 11:19 PM)menator01 Wrote: 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'}}
for v in currentuser.values():
print(f'|{v["uid"]}|{v["username"]}|{v["shell"]}') Output: |0|root|/bin/bash
|1|daemon|/usr/sbin/nologin
|2|bin|/usr/sbin/nologin
|1000|nsa|/bin/bash
Thanks for the reply i appreciate it. It works fine,
but i want this symbol | in the columns to align with eachother.
Posts: 9
Threads: 3
Joined: May 2021
(May-03-2021, 01:54 AM)bowlofred Wrote: 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
Thanks for the reply appreciate it, but i want to do it without import,
and i want the output to look like i requested.
Posts: 1,583
Threads: 3
Joined: Mar 2020
If you don't want to import, then you'll have to code it up.
Use len(str) to find the length of each of the elements in each column. Then use format strings to print each of the elements from that column in the correct length.
formats = ["<20", 10, 5]
data = [15, "Hello", "/bin/sh"]
print("".join(f"| {d:{f}}" for d,f in zip(data,formats))) Output: | 15 | Hello | /bin/sh
Posts: 1,950
Threads: 8
Joined: Jun 2018
You can pass formatting with f-strings so little adjustment to menator01 code would deliver required result (numbers are aligned to right and text to left which is canonical way to display these datatypes in table style):
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'}}
for value in currentuser.values():
print(f"{value['uid']:>8} {value['username']:<12} {value['shell']:<15}") This will print:
Output: 0 root /bin/bash
1 daemon /usr/sbin/nologin
2 bin /usr/sbin/nologin
1000 nsa /bin/bash
I personally like this type of display better than separated with | . However, if you prefer that you can add these into f-string quite easily:
for value in currentuser.values():
print(f"| {value['uid']:>8} | {value['username']:<12} | {value['shell']:<15}") This will deliver:
Output: | 0 | root | /bin/bash
| 1 | daemon | /usr/sbin/nologin
| 2 | bin | /usr/sbin/nologin
| 1000 | nsa | /bin/bash
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 1,144
Threads: 114
Joined: Sep 2019
Another possibility
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'}}
for v in currentuser.values():
print(f'|{v["uid"]}{" ".rjust(10-len(str(v["uid"])))}|{v["username"]}{" ".rjust(10-len(v["username"]))}|{v["shell"]}') Output: |0 |root |/bin/bash
|1 |daemon |/usr/sbin/nologin
|2 |bin |/usr/sbin/nologin
|1000 |nsa |/bin/bash
Posts: 9
Threads: 3
Joined: May 2021
(May-03-2021, 07:50 AM)perfringo Wrote: You can pass formatting with f-strings so little adjustment to menator01 code would deliver required result (numbers are aligned to right and text to left which is canonical way to display these datatypes in table style):
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'}}
for value in currentuser.values():
print(f"{value['uid']:>8} {value['username']:<12} {value['shell']:<15}") This will print:
Output: 0 root /bin/bash
1 daemon /usr/sbin/nologin
2 bin /usr/sbin/nologin
1000 nsa /bin/bash
I personally like this type of display better than separated with | . However, if you prefer that you can add these into f-string quite easily:
for value in currentuser.values():
print(f"| {value['uid']:>8} | {value['username']:<12} | {value['shell']:<15}") This will deliver:
Output: | 0 | root | /bin/bash
| 1 | daemon | /usr/sbin/nologin
| 2 | bin | /usr/sbin/nologin
| 1000 | nsa | /bin/bash
Thank you this worked well!
Posts: 9
Threads: 3
Joined: May 2021
Thank you all for your replies, the output is as i wanted it to print out.
|