0

I have the output as

app_39    281   33    94332  21228 ffffffff afd0c51c S abc
app_39    292   33    103864 30060 ffffffff afd0c51c S cvc.notes
app_39    303   33    94332  21344 ffffffff afd0c51c S cocccs:syncHandler
app_39    312   33    94332  21404 ffffffff afd0c51c S cccs:cacheHandler
app_39    321   33    94332  21344 ffffffff afd0c51c S cocccs:dailyReceiver

This whole output is stored in a variabble called outresult

How can I use python to extract the 2nd word of the second line? that is 292 and store it in a variable?

2
  • Do you have this output as a list? As a multiline string? As a file or a stream like stdin? Commented Mar 1, 2013 at 6:53
  • No, it is not STO-RED IN A VA-RIA-B-LE gna gna gna. There are no variables get out of it by Python. There are only identifiers and objects. Commented Mar 1, 2013 at 7:25

2 Answers 2

8
output.split('\n')[1].split()[1]

No regex needed.

Sign up to request clarification or add additional context in comments.

2 Comments

No regex needed, and now you got 0 problems +1.
.split(None, 2)[1] may be slightly more efficient as you only actually require 2 splits to get what you want.
0
ss = '''app_39    281   33    94332  21228 ffffffff afd0c51c S abc
app_39    292   33    103864 30060 ffffffff afd0c51c S cvc.notes
app_39    303   33    94332  21344 ffffffff afd0c51c S cocccs:syncHandler
app_39    312   33    94332  21404 ffffffff afd0c51c S cccs:cacheHandler
app_39    321   33    94332  21344 ffffffff afd0c51c S cocccs:dailyReceiver'''

def lect(inp,nline,nw):
    gen = (xli.split() for i,xli in enumerate(inp.splitlines()) if i==nline-1)
    return gen.next()[nw-1]


print lect(ss,2,2)

Comments

Your Answer

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.