The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Split
Discuss Split in the Python Programming forum on Dev Shed. Split Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 4th, 2004, 07:39 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
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
|

June 4th, 2004, 08:28 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
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
|

June 4th, 2004, 08:58 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

June 4th, 2004, 09:37 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
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
|

June 4th, 2004, 10:31 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
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
|

June 4th, 2004, 10:52 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
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()
|

June 4th, 2004, 11:10 AM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
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
|

June 5th, 2004, 05:57 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
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
|

June 5th, 2004, 09:06 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
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
|

June 5th, 2004, 09:09 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
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.
|

June 5th, 2004, 09:28 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
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
|

June 5th, 2004, 09:49 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
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.
|

June 5th, 2004, 10:22 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
Thanks mark
|

June 5th, 2004, 12:37 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

June 5th, 2004, 01:28 PM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
Yes it's working
Thank you so much mark
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|