2

I am getting command line output in below format

server
3 threads started
1.1.1.1 ONLINE at SUN
 version: 1.2.3.4
 en: net
1.1.1.2 ONLINE at SUN
 version: 1.2.3.5
 en: net
1.1.1.3 OFFLINE at SUN
 version: 1.2.3.6
 en: net
File: xys
high=600
low=70
name=lmn

I want parsed output like

l1 = [
 {
 "1.1.1.1": {
 "status": "ONLINE",
 "version": "1.2.3.4",
 "en": "net"
 },
 "1.1.1.2": {
 "status": "ONLINE",
 "version": "1.2.3.5",
 "en": "net"
 },
 "1.1.1.3": {
 "status": "OFFLINE",
 "version": "1.2.3.6",
 "en": "net"
 }
 }
]
l2 = {
 "File": "xys",
 "high": 600,
 "low": 70,
 "name": "lmn"
}

I am getting all this in a string. I Have split string by \n and created a list and then From "File" keyword created 2 lists of the main list. Then parsed both lists separately.

index = [i for i in range(len(output)) if "File" in output[i] ]
if index:
 list1 = output[:index[0]]
 list2 = output[index[0]:]

Is there any other more efficient way to parse this output.

petezurich
10.3k10 gold badges48 silver badges63 bronze badges
asked Jan 12, 2020 at 6:41
3
  • removing pandas tag as it is not relevant to the question Commented Jan 12, 2020 at 6:48
  • show your current code and explain why you think it is inefficient and what's the bottleneck Commented Jan 12, 2020 at 6:54
  • @MarcinOrlowski There is no bottleneck here, I just want to know is there any other better way than what am I doing. Commented Jan 12, 2020 at 7:08

2 Answers 2

1

What you did would work alright.

How much you should worry about this would depend on if this is just some quick setup being done for a few automated tests or if this code is for a service in an enterprise environment that has to stay running, but the one thing I would be worried about is what happens if File: ... is no longer the line that follows the IP addresses. If you want to make sure this does not throw of your code, you could go through the string line by line parsing it.

You would need your parser to check for all of the following cases:

  • The word server
  • The comments following the word server about how many threads where started
  • Any other comments after the word server
  • The IP address (regex is your friend)
  • The indented area that follows having found an IP addresses
  • key value pairs separated with a colon
  • key value pairs separated with an equals sign

But in all reality, I think what you did looks great. It's not that hard to change your code from searching for "File" to something else if that need ever arises. You will want to spend a little bit of time verifying that it appears that "File" does always proceed the IP addresses. If reliability is super important, then you will have some additional work to do in protecting yourself from running into problems later on if the order things come in is changes on you.

answered Jan 12, 2020 at 8:30
Sign up to request clarification or add additional context in comments.

Comments

1

The solution provided below does not need to use the number of server threads running, as it can keep track of the thread number by removing all metadata preceding and following the threads' information:

with open("data.txt", "r") as inFile:
 lines = [line for line in inFile]
 lines = [line for line in lines[2:] if line != '\n']
 threads = lines[:-4]
 meta = lines[-4:]
 l1 = []
 l2 = {}
 for i in range(0,len(threads),3):
 status = threads[i]
 version = threads[i+1]
 en = threads[i+2]
 status = status.split()
 name = status[0]
 status = status[1]
 version = version.split()
 version = version[1].strip()
 en = en.split()
 en = en[1].strip()
 l1.append({name : {'status' : status, "version" : version, "en" : en}})
 fileInfo = meta[0].strip().split(": ")
 l2.update({fileInfo[0] : fileInfo[1]})
 for elem in meta[1:]:
 item = elem.strip().split("=")
 l2.update({item[0] : item[1]})

The result will be:

For l1:

[{'1.1.1.1': {'status': 'ONLINE', 'version': '1.2.3.4', 'en': 'net'}}, {'1.1.1.2': {'status': 'ONLINE', 'version': '1.2.3.5', 'en': 'net'}}, {'1.1.1.3': {'status': 'OFFLINE', 'version': '1.2.3.6', 'en': 'net'}}]

For l2:

{'File': 'xys', 'high': '600', 'low': '70', 'name': 'lmn'}
answered Jan 12, 2020 at 11:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.