|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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. |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
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()
|
|
#7
|
||||
|
||||
|
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
__________________
IE QUOTE | PHP Manual | Google | C/C++ Compiler | Linux Tutorials | General Stuff Game Dev |
|
#8
|
|||
|
|||
|
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 |
|
#9
|
|||
|
|||
|
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 |
|
#10
|
||||
|
||||
|
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. |
|
#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 |
|
#12
|
||||
|
||||
|
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. |
|
#13
|
|||
|
|||
|
Thanks mark
|
|
#14
|
||
|