Thread: line

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    10

    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
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    10
    How to the totals

    Thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    10
    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
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    10
    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
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    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.
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    10
    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
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    10
    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
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    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.

    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?
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    10
    Hi all
    it's working
    Thanks for the help

IMN logo majestic logo threadwatch logo seochat tools logo