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

    Join Date
    May 2004
    Posts
    45
    Rep Power
    11

    How to get correct info


    Hi all
    Here is contents of detlog and where .abcd is the user id and .a till | is the context

    Thursday, 04/03/1999 10:46:00|0|112|.abcd.a.manual.re.gtre.tryu.trds|0.10
    Thursday, 04/03/1999 10:46:06|0|112|.abcd.a.manual.re.gtre.tryu.trds|1.10
    Thursday, 04/03/1998 10:46:30|0|112|.cdef.z.auto.ma.tic.la.bou|0.50
    Thursday, 04/03/1996 10:47:54|0|120|.cdef.z.auto.ma.tic.la.bou|10.0
    Thursday, 04/03/2000 10:48:00|0|121|.cdef.z.auto.ma.tic.la.bou|0.90


    What i am trying to do is just enter the user id and it will pick all the relevent info of the user id on this given file

    example
    enter user id you type "cdef" and it should bring all the details of "cdef" like below

    Thursday, 04/03/1998 10:46:30|0|112|.cdef.z.auto.ma.tic.la.bou|0.50
    Thursday, 04/03/1996 10:47:54|0|120|.cdef.z.auto.ma.tic.la.bou|10.0
    Thursday, 04/03/2000 10:48:00|0|121|.cdef.z.auto.ma.tic.la.bou|0.90

    but my code I'll have to type the whole entry such as

    .cdef.z.auto.ma.tic.la.bou

    do anyone know how to overcome this problem

    Thanks
    Code:
    import sys, os, re
    path = 'c:/detlog/detlog.log'#nettest1.log'#print3.log'
    count=0
    user_id=raw_input("Enter user id: ")
    number = re.compile('^[\d\n\.]+$')
    for line in file(path):
        line = line.split('|')
        if user_id ==(line[-3]):
            if line[-2].isdigit() or line[-2]=='.':
                continue
            if number.match(line[-2]):
                count+=float(eval(line[-2]))
                print ' ' .join(line)
    print " \n\t Total for %s is %.2f" % (user_id,count)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    Code:
    if user_id ==(line[-3]):
    Becomes

    Code:
    if user_id in line[-3]:
    ---

    Code:
            if line[-2].isdigit() or line[-2]=='.':
                continue
    What's the point?
    If the line is entirely made of digits, the test succeeds and the code continues.
    If the line contains only a single dot, the test succeeds and the code continues.
    If the line contains anything else (e.g. a number with a dot in it), the test fails and ... the code continues.
    (and you're applying it to line[-2] which is text).

    float(eval(line[-2])) is also redundant - the purpose of float() is to convert a string containing a float ("3.1") into a float type (3.1). The eval is not needed, and may prove harmful if there is some arbitrary and-or malicious code in the file.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    11
    Sorry the line[-1] and not -2 and user_id is line[-2]
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Is not the same code from the other thread? Only now you want it to do something different .

    Anyway, the if-continue block is more than redundant since in theory it will stop you're code ever getting to the match(). Which means it wont even work .

    You might want to read though the Python tutorial again since there seems to be a lot that you're missing.

    http://www.python.org/doc/2.3.4/tut/tut.html

    And, if you have any problems understanding anything just ask and i'll make sure you understand it. No one minds helping as long as you put the effort in yourself .

    Take care,

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

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    You might want to read though the Python tutorial again since there seems to be a lot that you're missing.
    Like comments and spacing.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    11
    Hi mark , sfb
    The reason i used the if- continue i want to filter the below lines which is also in the file
    Thursday, 04/03/1999 10:46:06|0|112|.abcd.a.manual.re.gtre.tryu.trds|ee1.10|
    Thursday, 04/03/1998 10:46:30|0|112|.cdef.|.
    Thursday, 04/03/1996 10:47:54|0|120|156780
    Thursday, 04/03/1996 10:47:54|0|120|\na

    Thanks
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    This may be obvious to some, but, how about tweeking the regex so it doesn't match those line endings that you dont want? Check out the re module, and google for some regex tutorials. These will probably be perl but that doesn't matter. You'll see why when you read though docs on the re module.

    http://www.python.org/doc/current/lib/module-re.html

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


IMN logo majestic logo threadwatch logo seochat tools logo