By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,276 Members | 1,951 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,276 IT Pros & Developers. It's quick & easy.

how to get a specific words from the line

P: 2
01JAN 06:49:30.439 WARNING - Entered Zone - Operator = 5, Body Part = Body, u = 75830967.07, v = 45233540.51, w = 0.00, delta = 3

From the above line how to print Entered zone and delta
3 Weeks Ago #1
Share this Question
Share on Google+
3 Replies


P: 94
If the input format is always constant, split the input data using "split".
Expand|Select|Wrap|Line Numbers
  1. val = input('')
  2. l = val.split(',')
  3. m = l[0].split('=')
  4. n = l[5].split('=')
  5. x = m[1]
  6. y = n[1]
  7. print(x,y)
  8.  
If there is a line feed code after delta = x, the following code works.
Expand|Select|Wrap|Line Numbers
  1. f = open('filename')
  2. val = f.readline()
  3. while val:
  4.   l = val.split(',')
  5.   m = l[0].split('= ')
  6.   n = l[5].split('= ')
  7.   if ('Entered Zone' in m[0]):
  8.       x = m[1]
  9.       y = n[1]
  10.       print(x,y)
  11.   val = f.readline()
  12. f.close
  13.  
One space after "delta = x" followed by the next data.
Expand|Select|Wrap|Line Numbers
  1. f = open('filename')
  2. val = f.readline()
  3. f.close
  4. val2 = val.replace(' ',',')
  5. word = val2.split(',')
  6. flg1 = False
  7. flg2 = False
  8. enterd = ''
  9. delta = ''
  10. i = 0
  11. for elem in word:
  12.     if word[i] == 'Entered':
  13.         enterd = word[i+5]
  14.         flg1 = True
  15.     if flg1 == True and word[i] == 'delta':
  16.         delta = word[i+2]
  17.         flg2 = True
  18.     if flg1 and flg2:
  19.         print(enterd,delta)
  20.         flg1 = False
  21.         flg2 = False
  22.     i = i + 1
  23.  
3 Weeks Ago #2

P: 2
01JAN 06:49:30.439 WARNING - Entered Zone - Operator = 5, Body Part = Body, u = 75830967.07, v = 45233540.51, w = 0.00, delta = 3 01JAN 06:49:30.439 WARNING - Entered Zone - Operator = 5, Body Part = Body, u = 75830967.07, v = 45233540.51, w = 0.00, delta = 1 01JAN 06:49:30.439 WARNING - EXITED - Operator = 5, Body Part = Body, u = 75830967.07, v = 45233540.51, w = 0.00, delta = 2 01JAN 06:49:30.439 WARNING - Entered Zone - Operator = 5, Body Part = Body, u = 75830967.07, v = 45233540.51, w = 0.00, delta = 4

FILE consists of those lines i have get the lines containing Entered Zone and print the delta value
3 Weeks Ago #3

P: 94
It look like there is no line break and there is no comma only after delta = x, so it is impossible with the previous post way.
I think that After "delta = x", there will be an operation to separate by "split" with a space.
3 Weeks Ago #4

Post your reply

Sign in to post your reply or Sign up for a free account.