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

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

Colloid Snake
100+
P: 144
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
Share this Question
Share on Google+
6 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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
100+
P: 144
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
Expert Mod 2.5K+
P: 2,851
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

Expert 100+
P: 511
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
Expert Mod 2.5K+
P: 2,851
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

Expert 100+
P: 511
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.