471,337 Members | 1,242 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

String - How to

Hi,

Here's a result from a ping:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Pinging 10.2.3.6 with 32 bytes of data:
  3.  
  4.  
  5. Request timed out.
  6.  
  7. Ping statistics for 10.2.3.6:
  8.  
  9.     Packets: Sent = 1, Received = 1, Lost = 1 (100% loss),
  10.  
  11.  
I`m trying to filter only the number before : loss in this case: 100%,
but I`m doing something wrong obviously because I receive:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <_sre.SRE_Match object at 0x00C461E0>
  3.  
  4.  

Here is my code, what do you suggest ?

Expand|Select|Wrap|Line Numbers
  1.  
  2. def run(self):
  3.       pingaling = os.popen("ping "+self.ip"r")
  4.  
  5. # This code is what each parallel thread does
  6. # the 'run' method is triggered when 'start' is called
  7.       while 1:
  8.          line = pingaling.read()
  9.          line = pingaling.read() 
  10.          mo = re.search("loss", line) 
  11.          if not line: break
  12.          print
  13.          print mo 
  14.  
  15.  
Sep 14 '07 #1
6 1326
ilikepython
844 Expert 512MB
Hi,

Here's a result from a ping:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Pinging 10.2.3.6 with 32 bytes of data:
  3.  
  4.  
  5. Request timed out.
  6.  
  7. Ping statistics for 10.2.3.6:
  8.  
  9.     Packets: Sent = 1, Received = 1, Lost = 1 (100% loss),
  10.  
  11.  
I`m trying to filter only the number before : loss in this case: 100%,
but I`m doing something wrong obviously because I receive:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <_sre.SRE_Match object at 0x00C461E0>
  3.  
  4.  

Here is my code, what do you suggest ?

Expand|Select|Wrap|Line Numbers
  1.  
  2. def run(self):
  3.       pingaling = os.popen("ping "+self.ip"r")
  4.  
  5. # This code is what each parallel thread does
  6. # the 'run' method is triggered when 'start' is called
  7.       while 1:
  8.          line = pingaling.read()
  9.          line = pingaling.read() 
  10.          mo = re.search("loss", line) 
  11.          if not line: break
  12.          print
  13.          print mo 
  14.  
  15.  
Maybe something like this:
Expand|Select|Wrap|Line Numbers
  1. line = "Packets: Sent = 1, Received = 1, Lost = 1 (100% loss),"
  2. patt = re.compile(r"\([0-9\.]+% loss\)")
  3. l = re.findall(patt, line)
  4. print l[0].split("%")[0][1:]
  5.  
Sep 14 '07 #2
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. >>> line = "Packets: Sent = 1, Received = 1, Lost = 1 (100% loss),"
  2. >>> import re
  3. >>> m = re.search(r"\(([0-9\.]+)% loss\)", line)
  4. >>> m.group(1)
  5. '100'
  6. >>> 
Sep 14 '07 #3
ilikepython
844 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. >>> line = "Packets: Sent = 1, Received = 1, Lost = 1 (100% loss),"
  2. >>> import re
  3. >>> m = re.search(r"\(([0-9\.]+)% loss\)", line)
  4. >>> m.group(1)
  5. '100'
  6. >>> 
Much easier, thanks barton.
Sep 14 '07 #4
Thanks for the replies, I appreciate a lot.
I will try this during the weekend. I will get back to you on monday
if I need a little help but I think that this will be good enough : )

tks, tks, tks.

Charlie of Bolton!
Sep 14 '07 #5
bartonc
6,596 Expert 4TB
Much easier, thanks barton.
That's actually your regex with () added to group the sought-after part.
I'm still not very proficient at writing the regex part, so thank you for that.
Sep 14 '07 #6
ghostdog74
511 Expert 256MB
Hi,

Here's a result from a ping:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Pinging 10.2.3.6 with 32 bytes of data:
  3.  
  4.  
  5. Request timed out.
  6.  
  7. Ping statistics for 10.2.3.6:
  8.  
  9.     Packets: Sent = 1, Received = 1, Lost = 1 (100% loss),
  10.  
  11.  
I`m trying to filter only the number before : loss in this case: 100%,
but I`m doing something wrong obviously because I receive:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <_sre.SRE_Match object at 0x00C461E0>
  3.  
  4.  

Here is my code, what do you suggest ?

Expand|Select|Wrap|Line Numbers
  1.  
  2. def run(self):
  3.       pingaling = os.popen("ping "+self.ip"r")
  4.  
  5. # This code is what each parallel thread does
  6. # the 'run' method is triggered when 'start' is called
  7.       while 1:
  8.          line = pingaling.read()
  9.          line = pingaling.read() 
  10.          mo = re.search("loss", line) 
  11.          if not line: break
  12.          print
  13.          print mo 
  14.  
  15.  
how about some basic string manipulation without regular expressions?
Expand|Select|Wrap|Line Numbers
  1. >>> line = "Packets: Sent = 1, Received = 1, Lost = 1 (100% loss),"
  2. >>> startopenbracket = line.find("(")
  3. >>> startloss = line.find("loss")
  4. >>> print line[startopenbracket+1:startloss]
  5. 100%
  6.  
Sep 15 '07 #7

Post your reply

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

Similar topics

16 posts views Thread by Krakatioison | last post: by
5 posts views Thread by Stu Cazzo | last post: by
9 posts views Thread by John F Dutcher | last post: by
10 posts views Thread by Angus Leeming | last post: by
2 posts views Thread by Andrew | last post: by
29 posts views Thread by zoro | last post: by
2 posts views Thread by Badass Scotsman | last post: by
15 posts views Thread by morleyc | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.