473,218 Members | 1,761 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,218 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 2092
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: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.