Thread: Split

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

    Join Date
    May 2004
    Posts
    45
    Rep Power
    11

    Split


    Dear All

    The following is my file

    see the world 2.50,see the moon 3.50, see the sun 6.50, see the sea 1.50, ewtrt trtr rtty 2.00, reyhrtjuytj dsafd dsfgg 6.50 and on

    Now i want to split the lines as follow and add the total

    see the world 2.50
    see the moon 3.50
    see the sun 6.50
    see the sea 1.50
    ewtrt trtr rtty 2.00
    reyhrtjuytj dsafd dsfgg 6.50

    total=22.50
    is it possible?

    Thanks
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Quick answer, yes it is... i have to eat right now but i'll give you a fully explained example in a little while.

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

  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    here we go, what we're gonna do here is write a small script that takes a string in the desired format and parses that string: first splitting it on ', ' then iterating over these results, split the line again in order to find any the numbers inside them - try and converting and adding the number to 'result'.

    Code:
    #!/usr/bin/env python
    
    source = 'see the world 2.50, see the moon 3.50, see the sun 6.50, see the sea 1.50, ewtrt trtr rtty 2.00, reyhrtjuytj dsafd dsfgg 6.50 and on'
    result = 0.00
    
    for values in source.split(', '):
        for number in values:
            try:
                result = result + float(number)
                break
            except ValueError:
                pass
        print number
    print 'Total =', result
    Note: this is untested but it should do what you want. If it doesn't just let me know whats wrong and i'll try my best to fix the problem.

    Hope this helps,

    Mark.
    Last edited by netytan; June 4th, 2004 at 10:40 AM.
    programming language development: www.netytan.com Hula

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

    Join Date
    May 2004
    Posts
    45
    Rep Power
    11

    split


    My fault please ignore the previous example her is the problem and my code

    I have a file in my c:\drive called nlog
    and the contents of nlog are as follow

    Thursday,04/03/200411:50:00|0|11|.wd313.wew.rtret.ffgbfg.fbvfdbgg.fdb.|0.10|,Thursday,04/03/200411:50:00|0|11|.345.fgf.fgfhh.fdhfgh.m.rty|0.10|,Thursday,04/03/200411:50:00|0|11|.345.fgf.fgfhh.fdhfgh.m.rty|1.50|,Thursday,04/03/200411:50:00|0|11|.345.fgf.fgfhh.fdhfgh.m.rty|0.90|,Thursday,04/03/200411:50:00|0|11|.345.fgf.fgfhh.fdhfgh.m.rty|1.00|,

    i want to split the lines as follow
    Thursday,04/03/200411:50:00|0|11|.345.fgf.fgfhh.fdhfgh.m.rty|0.10|,

    Thursday,04/03/200411:50:00|0|11|.345.fgf.fgfhh.fdhfgh.m.rty|0.90|,

    Thursday,04/03/200411:50:00|0|11|.345.fgf.fgfhh.fdhfgh.m.rty|1.50|, and on

    and count the amount in the |*.** |

    Thanks

    Code:
    import os
    os.chdir('/')
    os.chdir('c:\\nlog')
    print os.getcwd()
    infile=open("""nlog.log""",'r',)
    infile1=infile.readlines()
    infile1=infile1.split(',')
    print infile1,
    infile.close()
    Last edited by pyton; June 4th, 2004 at 09:44 AM. Reason: Previous Error
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Is the format of each entry the same? I presume it is, since it looks like a logfile of some sort.

    In that case I think the simplest thing to do is this:

    1) split on '|'

    Code:
    >>> s = "Thursday,04/03/200411:50:00|0|11|.wd313.wew.rtret.ffgbfg.fbvfdbgg.fdb.|0.10|,Thursday,04/03/200411:50:00|0|11|.345.fgf.fgfhh.fdhfgh.m.rty|0.10|,Thursday,04/03/200411:50:00|0|11|.345.fgf.fgfhh.fdhfgh.m.rty|1.50|,Thursday,04/03/200411:50:00|0|11|.345.fgf.fgfhh.fdhfgh.m.rty|0.90|,Thursday,04/03/200411:50:00|0|11|.345.fgf.fgfhh.fdhfgh.m.rty|1.00|,"
    >>> lst = s.split('|')
    >>> for i,v in enumerate(lst): print i,v
    ... 
    0 Thursday,04/03/200411:50:00
    1 0
    2 11
    3 .wd313.wew.rtret.ffgbfg.fbvfdbgg.fdb.
    4 0.10
    5 ,Thursday,04/03/200411:50:00
    6 0
    7 11
    8 .345.fgf.fgfhh.fdhfgh.m.rty
    9 0.10
    10 ,Thursday,04/03/200411:50:00
    11 0
    12 11
    13 .345.fgf.fgfhh.fdhfgh.m.rty
    14 1.50
    15 ,Thursday,04/03/200411:50:00
    16 0
    17 11
    18 .345.fgf.fgfhh.fdhfgh.m.rty
    19 0.90
    20 ,Thursday,04/03/200411:50:00
    21 0
    22 11
    23 .345.fgf.fgfhh.fdhfgh.m.rty
    24 1.00
    25 ,
    then get every 5th element, starting with the one at position 4. You can do this with an extended slice:

    Code:
    >>> lst[4::5]
    ['0.10', '0.10', '1.50', '0.90', '1.00']
    you now have a list of strings. You need to convert them to floats so that you can add them up. There are several ways to do this, but the simplest is with a list substitution.

    Code:
    >>> values = [float(x) for x in lst[4::5] ]
    >>> values
    [0.10000000000000001, 0.10000000000000001, 1.5, 0.90000000000000002, 1.0]
    Finally add up the values. You can use the built in function 'sum' to add up a sequence of numbers.

    Code:
    >>> sum(values)
    3.6000000000000001
    >>>
    If you want, you can put it all together into a single expression:

    Code:
    >>> sum( [ float(x) for x in s.split('|')[4::5] ] )
    3.6000000000000001
    >>>
    Simple, eh?

    Dave - The Developers' Coach
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    11
    First of all thanks for the simple code

    My problem is how do i read the contents from a file, split and add the total

    this is the error i am getting

    c:\nlog

    Traceback (most recent call last):
    File "F:\Pyth2\netprint test2.py", line 7, in -toplevel-
    infile1=infile1.split('|')
    AttributeError: 'list' object has no attribute 'split'

    this is my code
    Code:
    import os
    os.chdir('/')
    os.chdir('c:\\nlog')
    print os.getcwd()
    infile=open("""nlog.log""",'r',)
    infile1=infile.readlines()
    infile1=infile1.split(' | ')
    print infile1,
    infile.close()
  12. #7
  13. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Code:
    import os
    
    os.chdir('/')
    os.chdir('c:\\nlog')
    
    print os.getcwd( )
    
    for infile in file( """nlog.log""" ):
        infile1, infile2  =  infile.strip().split( " | " )
        print infile1, infile2
    try that
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    11
    Thanks for your help Xlordt

    I tried the code but getting the following error

    Traceback (most recent call last):
    File "C:\Pyth2\infline.py", line 9, in -toplevel-
    infile1, infile2 = infile.strip().split( " | " )
    ValueError: unpack list of wrong size
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    11
    Code:
    import os
    os.chdir('/')
    os.chdir('c:\\nlog')
    
    print os.getcwd( )
    
    for infile in file( """nlog.log""" ):
        infile1 =  infile.split('|')
        for i,v in enumerate(infile1):
           print i,v
        
    print infile1[4::5]
    answer

    ['2.95']

    The problem is the code is counting 0 to 5 for every line and only displaying the last amount which is 2.95

    my intention is to count the lines from 0 till end of file like the code above as devcoach and display all the amount like [1.60, 195, 295]

    i am simply trying to modify the code from devcoach and xlordt to meet my needs (important i am picking the details from a file stored in c:\drive\nlog and the file is called nlog.log
    can some one help me

    thanks
  18. #10
  19. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Why does this happen? This is rather like trying yo fit three coints into a slit designed to take only one. So if you havn't guessed, you get this error when you try to assign too may values to too few (or too many) variables . Imagin if you had a tuple that looked something like this:

    Code:
    >>> t = (1, 2, 3, 4)
    and you want to unpack -assign the values in the sequence to the variables on the left - these values into there respective variables.

    Code:
    >>> a, b  = t
    As you can imagin this isn't pretty, and would cause the same error you posted since there are just too many values. But if you had the right number of vairables?

    Code:
    >>> a, b, c, d = t
    >>> a
    1
    >>> b
    2
    >>> c
    3
    >>> d
    4
    >>>
    Then everything fits where its surposed too and everyone is happy .

    So, in your example, splitting the line creates a list, which contains too many values to assign too the number of variables causing Python to puck out one of its lovly errors onto your screen

    Hope this makes sence; have fun!

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

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

    Join Date
    May 2004
    Posts
    45
    Rep Power
    11
    Hello netytan

    I understood your code but how do i fit in to my need could you please look at my codding and fix the errors

    Thanks
  22. #12
  23. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Of course i'd be happy to help, but can you please attach the source file - the one with some data so that i can test it.

    Thanks,

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

  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    11
    Thanks mark
  26. #14
  27. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    This should work with the data you gave me, you'll have to test it yourself and tell me if there are any problems.

    Code:
    #!/usr/bin/env python
    
    path = 'nlong.log'
    list = []
    loop = 0
    
    for line in file(path):
        if line.strip():
            line = line[:-2]
            for part in line.split('|'):
                print '%02d %s' % (loop, part)
            list.append(float(part))
    
            loop = loop + 1
    
    print '\n\n[%s]' % sum(list)
    Mark.
    programming language development: www.netytan.com Hula

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

    Join Date
    May 2004
    Posts
    45
    Rep Power
    11
    Yes it's working
    Thank you so much mark

IMN logo majestic logo threadwatch logo seochat tools logo