Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need bit help in formatting
#1
Dear experts

I have python script which gives output like..The below is string

b"{'text': 'Cluster1 is healthy\nNode 123.4.0.3:8091 is Up\nNode 123.4.0.2:8091 is Up\nNode 123.4.0.4:8091 is Up'}\n{'text': 'Cluster2 is unhealthy\nNode 123.8.0.3:8091 is Down\nNode 123.8.0.2:8091 is Up\nNode 123.8.0.4:8091 is Up'}\n"

What I tried so far:
message =  b"{'text': 'Cluster1 is healthy\nNode 123.4.0.3:8091 is Up\nNode 123.4.0.2:8091 is Up\nNode 123.4.0.4:8091 is Up'}\n{'text': 'Cluster2 is unhealthy\nNode 123.8.0.3:8091 is Down\nNode 123.8.0.2:8091 is Up\nNode 123.8.0.4:8091 is Up'}\n"
message1 = message.rstrip('\n')
print(message1)
But I am getting like below
{'text': 'Cluster1 is healthy
Node 123.4.0.3:8091 is Up
Node 123.4.0.2:8091 is Up
Node 123.4.0.4:8091 is Up'}
{'text': 'Cluster2 is unhealthy
Node 123.8.0.3:8091 is Up
Node 123.8.0.2:8091 is Down
Node 123.8.0.4:8091 is Up'}

I need to format above like something below
Cluster1 is healthy
Node 123.4.0.3:8091 is Up
Node 123.4.0.2:8091 is Up
Node 123.4.0.4:8091 is Up

Cluster2 is unhealthy
Node 123.8.0.3:8091 is Up
Node 123.8.0.2:8091 is Down
Node 123.8.0.4:8091 is Up

Please help what additional things I need to do
Reply
#2
if you have control over the output of previous script - change it
Reply
#3
(Mar-15-2018, 05:10 PM)buran Wrote: if you have control over the output of previous script - change it

Yes i tried to do that. this was the original line

MESSAGE = {'text': '\n'.join(msg_list).lstrip('\n')}

for testing I changed to

MESSAGE = {'\n'.join(msg_list).lstrip('\n')}

Then i get something like below on screen when i tried to print

set(['Cluster1 is healthy\nNode 123.4.0.2:8091 is Up\nNode 123.4.0.3:8091 is Up\nNode 123.4.0.4:8091 is Up'])
set(['Cluster2 is unhealthy\nNode 123.8.0.2:8091 is Up\nNode 123.8.0.3:8091 is Down\nNode 123.8.0.4:8091 is Up'])

Do I need to iterate through text ?
Reply
#4
I guess your whole setup is wrong. Either make your other script to return proper python object(s) - dict, list, json, etc. or if it write to file - make it proper formated json file.
Do you get the result by printing to stdout and then in the second script reading the output from first script?
Reply


Forum Jump:

User Panel Messages

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