467,926 Members | 1,721 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,926 developers. It's quick & easy.

how to get a specific words from the line

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
Feb 3 '20 #1
  • viewed: 1245
Share:
3 Replies
128KB
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.  
Feb 3 '20 #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
Feb 3 '20 #3
128KB
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.
Feb 3 '20 #4

Post your reply

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

Similar topics

reply views Thread by Brian | last post: by
1 post views Thread by Toska | last post: by
5 posts views Thread by akelly_image | last post: by
11 posts views Thread by elrondrules | last post: by
1 post views Thread by sejal17 | last post: by
1 post views Thread by Marco Langes | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.