471,350 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Simple script to remove value from line, output printing twice at end

Colloid Snake
144 100+
Hello,

I'm running into an odd problem - well, at least I think it's odd, but that's probably because I have a Cygwin screen burned into my retinas from staring at it for so long. When I run my script below, the output lines print twice. I'm hoping it's something simple (and yet kind of not, because then I look like a fool... oh well), but hopefully a fresh pair of eyes might be able to help me.

Oh, and please feel free to comment on how I'm removing the data - I found a QAD way, but I'm guessing there might be a better way.

Expand|Select|Wrap|Line Numbers
  1. s_fileToParse = "/opt/www/status.html"
  2. s_SummaryFile = "/home/user/weeklyReport.txt"
  3. l_linesToRead = []
  4. i_configured = 0
  5. i_down = 0
  6. FILE = open(s_fileToParse,"r")
  7.  
  8. l_linesToRead = FILE.readlines()
  9.  
  10. for s_lines in l_linesToRead:
  11.   if s_lines.startswith("Total"):
  12.     # parse for number between <B> and </B>
  13.     s_lines = s_lines.lstrip("Total # Configured: <B>")
  14.     s_lines = s_lines.rstrip("</B><BR>\n")
  15.     i_configured = int(s_lines)
  16.   elif s_lines.startswith('# down:'):
  17.     # parse for number between <B> and </B>
  18.     s_lines = s_lines.lstrip('<B>')
  19.     s_lines = s_lines.rstrip('</z')
  20.     i_down = int(s_lines)
  21.  
  22. FILE.close()
  23. FILE = open(s_SummaryFile,"a")
  24. FILE.write("\nTotal # Configured: ")
  25. FILE.write(str(i_configured))
  26. FILE.write("\nTotal # Down: ")
  27. FILE.write(str(i_down))
  28. FILE.write("\nTotal # Reporting: ")
  29. FILE.write(str(i_configured-i_down))
  30. FILE.close()
  31.  
Nov 20 '07 #1
6 1856
bvdet
2,851 Expert Mod 2GB
Hello,

I'm running into an odd problem - well, at least I think it's odd, but that's probably because I have a Cygwin screen burned into my retinas from staring at it for so long. When I run my script below, the output lines print twice. I'm hoping it's something simple (and yet kind of not, because then I look like a fool... oh well), but hopefully a fresh pair of eyes might be able to help me.

Oh, and please feel free to comment on how I'm removing the data - I found a QAD way, but I'm guessing there might be a better way.

Expand|Select|Wrap|Line Numbers
  1. s_fileToParse = "/opt/www/status.html"
  2. s_SummaryFile = "/home/user/weeklyReport.txt"
  3. l_linesToRead = []
  4. i_configured = 0
  5. i_down = 0
  6. FILE = open(s_fileToParse,"r")
  7.  
  8. l_linesToRead = FILE.readlines()
  9.  
  10. for s_lines in l_linesToRead:
  11.   if s_lines.startswith("Total"):
  12.     # parse for number between <B> and </B>
  13.     s_lines = s_lines.lstrip("Total # Configured: <B>")
  14.     s_lines = s_lines.rstrip("</B><BR>\n")
  15.     i_configured = int(s_lines)
  16.   elif s_lines.startswith('# down:'):
  17.     # parse for number between <B> and </B>
  18.     s_lines = s_lines.lstrip('<B>')
  19.     s_lines = s_lines.rstrip('</z')
  20.     i_down = int(s_lines)
  21.  
  22. FILE.close()
  23. FILE = open(s_SummaryFile,"a")
  24. FILE.write("\nTotal # Configured: ")
  25. FILE.write(str(i_configured))
  26. FILE.write("\nTotal # Down: ")
  27. FILE.write(str(i_down))
  28. FILE.write("\nTotal # Reporting: ")
  29. FILE.write(str(i_configured-i_down))
  30. FILE.close()
  31.  
I don't see how it works at all.
Expand|Select|Wrap|Line Numbers
  1. elif s_lines.startswith('# down:'):
  2.     # parse for number between <B> and </B>
  3.     s_lines = s_lines.lstrip('<B>')
  4.     s_lines = s_lines.rstrip('</z')
  5.     i_down = int(s_lines)
If 's_lines.startswith('# down')' is True, s_lines.lstrip('<B>') would not do anything since '#' is the leading character. The sunsequent call to 'int()' would then fail because of the non-numeric characters. Following is a possible solution using the re module.
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. fn_read = "input.txt"
  4. fn_write = "output.txt"
  5. patt = re.compile(r'<B>\s*(\d+)\s+</B>')
  6.  
  7. '''Example strings:
  8. "Total # Configured: <B> 12345 </B><BR>\n"
  9. "# down: <B> 67890 </B>"
  10. '''
  11.  
  12. f = open(fn_read)
  13. for line in f:
  14.     if line.startswith('Total'):
  15.         i_configured = int(patt.search(line).group(1))
  16.     elif line.startswith('# down:'):
  17.         i_down = int(patt.search(line).group(1))
  18.  
  19. f.close()
  20.  
  21. f = open(fn_write, 'a')
  22. f.write("\nTotal # Configured: %d\nTotal # Down: %d\nTotal # Reporting: %d" % \
  23.         (i_configured, i_down, i_configured-i_down))
  24. f.close()
Nov 21 '07 #2
Colloid Snake
144 100+
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. fn_read = "input.txt"
  4. fn_write = "output.txt"
  5. patt = re.compile(r'<B>\s*(\d+)\s+</B>')
  6.  
  7. '''Example strings:
  8. "Total # Configured: <B> 12345 </B><BR>\n"
  9. "# down: <B> 67890 </B>"
  10. '''
  11.  
  12. f = open(fn_read)
  13. for line in f:
  14.     if line.startswith('Total'):
  15.         i_configured = int(patt.search(line).group(1))
  16.     elif line.startswith('# down:'):
  17.         i_down = int(patt.search(line).group(1))
  18.  
  19. f.close()
  20.  
  21. f = open(fn_write, 'a')
  22. f.write("\nTotal # Configured: %d\nTotal # Down: %d\nTotal # Reporting: %d" % \
  23.         (i_configured, i_down, i_configured-i_down))
  24. f.close()
Ah, much more elegant. I wasn't sure if a regex was a good choice here, but thank you very much for the help - works like a charm!
Nov 26 '07 #3
bvdet
2,851 Expert Mod 2GB
Ah, much more elegant. I wasn't sure if a regex was a good choice here, but thank you very much for the help - works like a charm!
Thanks for the feedback and you are welcome. If there is another way besides regex, you should probably try it. Regex seems to be the easiest in this case however.
Nov 26 '07 #4
ghostdog74
511 Expert 256MB
without regexp, you can just use simple string methods, eg index?

Expand|Select|Wrap|Line Numbers
  1. >>> s = """Total # Configured: <B> 12345 </B><BR> """
  2. >>> s.index("<B>")
  3. 20
  4. >>> s.index("</B>")
  5. 30
  6. >>> s[20:30]
  7. '<B> 12345 '
  8. >>> s[20+4:30]
  9. '12345 '
  10. >>> s[20+len("<B>"):30]
  11. ' 12345 '
  12.  
  13.  
Nov 28 '07 #5
bvdet
2,851 Expert Mod 2GB
without regexp, you can just use simple string methods, eg index?

Expand|Select|Wrap|Line Numbers
  1. >>> s = """Total # Configured: <B> 12345 </B><BR> """
  2. >>> s.index("<B>")
  3. 20
  4. >>> s.index("</B>")
  5. 30
  6. >>> s[20:30]
  7. '<B> 12345 '
  8. >>> s[20+4:30]
  9. '12345 '
  10. >>> s[20+len("<B>"):30]
  11. ' 12345 '
  12.  
  13.  
It's nice to "see" you again ghostdog74. This is a good alternative to a regex solution.
Nov 28 '07 #6
ghostdog74
511 Expert 256MB
It's nice to "see" you again ghostdog74. This is a good alternative to a regex solution.
hey bv. nice to see you are still here and active as ever. :)
Nov 28 '07 #7

Post your reply

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

Similar topics

38 posts views Thread by jrlen balane | last post: by
6 posts views Thread by R. Stormo | last post: by
reply views Thread by Daniel Sélen Secches | last post: by
27 posts views Thread by one man army | last post: by
6 posts views Thread by sathyashrayan | last post: by
24 posts views Thread by firstcustomer | last post: by
6 posts views Thread by Armel Asselin | last post: by
7 posts views Thread by ojsimon | 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.