Python Forum
Printing the output in columns
Thread Rating:
  • 3 Vote(s) - 3.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing the output in columns
#1
Hi Gurus,

I am trying to print all services running on my windows laptop as a "three column output"
and i have written the following code.

However, i am only getting only one line output.
Could someone help/point me to the right direction?

import re
from subprocess import check_output

with open('c:/users/dell/file.txt', 'w') as f:
    output=check_output("c:/windows/system32/sc query state= all").decode()
    print(output, file=f)

print('Service Name \t DISPLAY NAME \t\t\t STATE')
file=open('c:/users/dell/file.txt').readlines()

for i in file:
    one=re.match(r'(SERVICE_NAME:)\s(.*)',i)
    if one is not None:
        x=one.group(2)

for i in file:
    two=re.match(r'(DISPLAY_NAME:)\s(.*)',i)
    if two is not None:
        y=two.group(2)

for i in file:
    three=re.search(r'(STATE.*:)\s(.*)',i)
    if three is not None:
            z=three.group(2)

print(('%s \t %s \t %s')%(x,y,z))
Output:
>>> Service Name DISPLAY NAME STATE WwanSvc WWAN AutoConfig 4 RUNNING >>>
Reply
#2
You have to use lists:
x = [ ]
for i in file:
    x.append(...)
Reply
#3
your tabs do not match.
header has 4 tabs before State field,
detail has only two.
you need to use two for both, and then adjust tab width to match.
A better way would be to format each field for maximum width, or anticipated maximum width
and then print fields with fixed spacing between.

for a good tutorial, see: https://www.python-course.eu/python3_for...output.php
Reply
#4
#!python3
import re
from subprocess import check_output

with open('file.txt', 'w') as f:
    output=check_output("c:/windows/system32/sc query state= all").decode("utf-8", "ignore")
    print(output, file=f)
lines = open('file.txt').readlines()

x = [ ]
for line in lines:
    one=re.match(r'(SERVICE_NAME:)\s(.*)', line)
    if one:
        x.append(one.group(2))

y = [ ]
for line in lines:
    two=re.match(r'(DISPLAY_NAME:)\s(.*)', line)
    if two:
        y.append(two.group(2))

z = [ ]
for line in lines:
    three=re.search(r'(STATE.*:)\s(.*)', line)
    if three:
        z.append(three.group(2))

print('%-20s %-60s %-10s' % ("Service Name", "DISPLAY NAME", "STATE"))
for a,b,c in zip(x, y, z):
    print('%-20s %-60s %-10s' % (a, b, c))
#done
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing output without print usage susmith552 1 1,658 Feb-07-2020, 07:52 PM
Last Post: Clunk_Head
  output a list of random numbers 'x' columns wide adityavpratap 4 2,976 Jan-13-2020, 05:32 PM
Last Post: perfringo
  How do I stop this fast factorization program from printing beyond the 3rd output? Pleiades 6 3,847 Dec-07-2019, 08:43 PM
Last Post: Pleiades
  Printing lists in a table, rows and columns randy_shooflay 6 6,293 Sep-05-2018, 07:59 PM
Last Post: perfringo
  ciscolib cdp output list printing support anna 3 3,328 Jul-25-2018, 12:18 PM
Last Post: buran
  easysnmp output printing help anna 0 2,803 Apr-03-2018, 09:19 AM
Last Post: anna
  Output not printing when execute scripts in threads anna 1 2,730 Mar-21-2018, 05:08 PM
Last Post: woooee
  Write selected (random) columns to output-file anjohepa 0 2,335 Feb-27-2018, 02:19 PM
Last Post: anjohepa
  Paramiko output printing issue anna 3 16,027 Feb-06-2018, 08:34 AM
Last Post: anna
  telnet unexpected output printing anna 3 3,576 Jan-17-2018, 01:16 PM
Last Post: anna

Forum Jump:

User Panel Messages

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