Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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 June 4th, 2004, 07:39 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: 10
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

Reply With Quote
  #2  
Old June 4th, 2004, 08:28 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,537 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 17 m 47 sec
Reputation Power: 68
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
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


Reply With Quote
  #3  
Old June 4th, 2004, 08:58 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,537 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 17 m 47 sec
Reputation Power: 68
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
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.

Reply With Quote
  #4  
Old June 4th, 2004, 09:37 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: 10
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

Reply With Quote
  #5  
Old June 4th, 2004, 10:31 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,585 DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
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

Reply With Quote
  #6  
Old June 4th, 2004, 10:52 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: 10
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()

Reply With Quote
  #7  
Old June 4th, 2004, 11:10 AM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,573 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 11 h 9 m 33 sec
Reputation Power: 406
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
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

Reply With Quote
  #8  
Old June 5th, 2004, 05:57 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: 10
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

Reply With Quote
  #9  
Old June 5th, 2004, 09:06 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: 10
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

Reply With Quote
  #10  
Old June 5th, 2004, 09:09 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,537 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 17 m 47 sec
Reputation Power: 68
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
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.

Reply With Quote
  #11  
Old June 5th, 2004, 09:28 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: 10
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

Reply With Quote
  #12  
Old June 5th, 2004, 09:49 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,537 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 17 m 47 sec
Reputation Power: 68
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
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.

Reply With Quote
  #13  
Old June 5th, 2004, 10:22 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: 10
Thanks mark

Reply With Quote
  #14  
Old June 5th, 2004, 12:37 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,537 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 17 m 47 sec
Reputation Power: 68
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 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.

Reply With Quote
  #15  
Old June 5th, 2004, 01:28 PM
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: 10
Yes it's working
Thank you so much mark

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Split

Developer Shed Advertisers and Affiliates



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

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap