473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2099
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

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

Similar topics

38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
1
by: Randell D. | last post by:
HELP! I am determined to stick with this... I'm getting there... for those who haven't read my earlier posts, I'm createing what should be a simple function that I can call to check that...
6
by: R. Stormo | last post by:
I have a problem showing output that is comming from a script. If I make a script running at commandline it do work and everything are showing. But when I try to execute it from within my proggy...
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
6
by: sathyashrayan | last post by:
Dear group, Following is a exercise from a book called "Oreilly's practical C programming". I just wanted to do a couple of C programming exercise. I do have K and R book, but let me try some...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
6
by: Armel Asselin | last post by:
Hello, I'm searching for a simple command line tool to manipulate XML files. The idea would be commands such as that: xmanip-tool set /document/xpath/@name="value" remove //wrong-nodes add...
7
by: ojsimon | last post by:
Hi I found this script on a forum and have been trying to make it work, but all it returns is a blank screen, i tried using the debug error reporting but got nothing from that either just a blank...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.