Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 15th, 2004, 07:14 AM
pyton pyton is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 45 pyton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
line

All

Here is something i couldn't get it work
I wat to check end of the line and print the if it is number and avoid if not number. Following example
file called det.log and the contens are as follow

,today, 12/12/12,|10.56|.monday.tot.col|5.00
,today, 12/12/12,|10.56|.monday.tot.col|cer
'today, 12/12/12,|10.56|.monday.tot.col|a.00
'today, 12/12/12,|10.56|.monday.tot.col|5.a

result=5.00

Thanks

Reply With Quote
  #2  
Old July 15th, 2004, 07:43 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
This should be pretty easily. There are two ways i can think of to do this: by using regex to check if the string is a valid number or trying to convert the last part of each line to a number.

Code:
pseudocode
import re

number = re.compile('^[\d\n\.]+$') #or other regex to match numbers.

for line in file('det.log'):
    line = line.split('|')
    if number.match(line[-1]):
        ...
        do somthing with line.
        ...


Hope this helps,

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old July 15th, 2004, 08:11 AM
pyton pyton is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 45 pyton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
How to the totals

Thanks

Reply With Quote
  #4  
Old July 15th, 2004, 09:03 AM
pyton pyton is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 45 pyton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Another problem
if end of the is letters instead number it displays the letters too which i didn't want it to
today, 12/12/12,|10.56|.monday.tot.col|ccc|
thanks

Reply With Quote
  #5  
Old July 15th, 2004, 09:39 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Im pretty sure this has been covered before, not even a month ago maybe. Anyway, you need to convert the last part of the string to a number and if this is successful add it to the total. If not, just output the string.

Mark.

Reply With Quote
  #6  
Old July 15th, 2004, 09:48 AM
pyton pyton is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 45 pyton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Yes mark we did go through this before the reason i come back to the same subject is converting the string to number and omitting the letters are not working properly anyway you metioned you can do it in 2 ways how do you do it with regex


Thanks

Reply With Quote
  #7  
Old July 15th, 2004, 12:22 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
The code snippet above uses regex to check if the line contains a valid numer (the regex provided will work but it isnt very strict). After this regex line conferms that the string is what you want you still have to convert it to a float befor you can add up the total. What exactly is going wrong with the pogram?

Mark.

Reply With Quote
  #8  
Old July 15th, 2004, 08:05 PM
rebbit rebbit is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 84 rebbit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 7 m
Reputation Power: 5
instead of using a regex you could try casting the last string to an int and catch any exceptions raised when you hit a string, ie:

Code:

total = 0
for line in file('det.log'):
    line = line.split('|')
    try:
        num = int(line[-1])
        total += num
        print line
    except ValueError:
        pass


I'm not exactly sure what you want the output to be, but you get the idea.

Reply With Quote
  #9  
Old July 16th, 2004, 02:02 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Hey Rabbit,

Thats how we did it the first time, unfortunatly he says it isn't quite working out for him (not sure whats changed though). Aslo, these numbers are float() so you need to cast them as such.

The output should simply be the total of all the floating point numbers: ignoring any that dont match/cast. Pyton also wants the end for each line to be outputted. Not a problem, just print line[-1] before or after the try block.

Oh, and you'll also have to strip() any white space chars i.e. \n, from the end of he line or you're going to run into problems casting .

Have fun,

Mark.

Reply With Quote
  #10  
Old July 16th, 2004, 05:16 AM
pyton pyton is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 45 pyton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Hi mark,rabbit ....etc

Thank you all for your valuablee help and support

I have modified your code as follow
Code:
import re
path='c:/det.log'
number = re.compile('^[\d\n\.]+$') #or other regex to match numbers.

for line in file(path):
    line = line.split('|')
    if number.match(line[-1]):
       print ' '.join(line)
       count=eval(line[-1])
       total+=count
print '\t\t\t  Total for %s = %0.2f' % (path,total)


this code works fine untill it finds the nubers instead of string
i.e
today, 12/12/12,|10.56|.monday.tot.col|5.00
the above line works fine but if it finds a line
today, 12/12/12,|10.56|.monday.tot.col|567483 then it terminates I know i am converting "count=eval(line[-1]) to number but if the line[-1] is already a number then it drops the following error

Code:
count=eval(line[-1])
  File "<string>", line 1
    000275269
            ^
SyntaxError: invalid token


Thanks

Reply With Quote
  #11  
Old July 16th, 2004, 07:07 AM
pyton pyton is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 45 pyton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Hi all
I sorted all the above said problems and here is my code
Code:
import sys, os, re
path='c:/det.log'
total=0.00
trim='|'
number=re.compile('^[\d\n\.]+$')
for line in file(path):
    line=line.split(trim)
    if line[-1].isdigit():
        continue
    if line[-1]=='.':
        continue
    if number.match(line[-1]):
         print ' ' .join(line)
         count=eval(line[-1])
         total+=count
print '\t\t\t  Total for %s = %0.2f' % (path,total)


Is teher a way i can compact my coding i know it's not right for example from line 4 to 6. i am checking for numbers and "." if they are found at line[-1] then to continue.

Thanks all

Reply With Quote
  #12  
Old July 16th, 2004, 07:29 AM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 9
Code:
count=eval(line[-1])
  File "<string>", line 1
    000275269
            ^
SyntaxError: invalid token


Numbers beginning with 0 are interpreted as octal - you can't have the digit '9' in an octal number.

Your fix doesn't look like it would get around that.

Quote:
Is teher a way i can compact my coding i know it's not right for example from line 4 to 6. i am checking for numbers and "." if they are found at line[-1] then to continue.


Lines 4-6?
Code:
trim='|'
number=re.compile('^[\d\n\.]+$')
for line in file(path):

??


Code:
if line[-1].isdigit():
if number.match(line[-1]):


These are pretty much redundant - the regex will only match numbers, so it will do the same as the isdigit() test.

Code:
if line[-1]=='.':
    continue

This will continue if the last word in the string is '.' which isn't a number.

Perhaps...

Code:
import sys, os, re

path='c:/det.log'
total=0.00
number=re.compile('^[\d\n\.]+$')

for line in file(path):
    # Remove line endings and split into chunks
    line = line.strip().split('|')

    # Take the last chunk, and remove starting zeros
    temp = line[-1]
    temp = temp.lstrip('0')

    # If the chunk only contains numbers,
    # Convert it to a number and add to the total
    # or print the error message if conversion fails
    if number.match(temp):
        try:
            total+=float(count)
        except ValueError, err:
            print err

print '\t\t\t  Total for %s = %0.2f' % (path,total)


Would work?

Reply With Quote
  #13  
Old July 19th, 2004, 02:27 AM
pyton pyton is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 45 pyton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Hi all
it's working
Thanks for the help

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > line


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump