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
#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


Messages In This Thread
Printing the output in columns - by pythonbabe - Nov-21-2017, 04:28 AM
RE: Printing the output in columns - by heiner55 - Nov-21-2017, 06:10 AM
RE: Printing the output in columns - by Larz60+ - Nov-21-2017, 12:03 PM
RE: Printing the output in columns - by heiner55 - Nov-23-2017, 09:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing output without print usage susmith552 1 1,679 Feb-07-2020, 07:52 PM
Last Post: Clunk_Head
  output a list of random numbers 'x' columns wide adityavpratap 4 3,038 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,891 Dec-07-2019, 08:43 PM
Last Post: Pleiades
  Printing lists in a table, rows and columns randy_shooflay 6 6,363 Sep-05-2018, 07:59 PM
Last Post: perfringo
  ciscolib cdp output list printing support anna 3 3,349 Jul-25-2018, 12:18 PM
Last Post: buran
  easysnmp output printing help anna 0 2,826 Apr-03-2018, 09:19 AM
Last Post: anna
  Output not printing when execute scripts in threads anna 1 2,756 Mar-21-2018, 05:08 PM
Last Post: woooee
  Write selected (random) columns to output-file anjohepa 0 2,355 Feb-27-2018, 02:19 PM
Last Post: anjohepa
  Paramiko output printing issue anna 3 16,090 Feb-06-2018, 08:34 AM
Last Post: anna
  telnet unexpected output printing anna 3 3,611 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