|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
line
All
Here is something i couldn't get it work I wat to check end of the line and print the if it is number and avoid if not number. Following example file called det.log and the contens are as follow ,today, 12/12/12,|10.56|.monday.tot.col|5.00 ,today, 12/12/12,|10.56|.monday.tot.col|cer 'today, 12/12/12,|10.56|.monday.tot.col|a.00 'today, 12/12/12,|10.56|.monday.tot.col|5.a result=5.00 Thanks |
|
#2
|
||||
|
||||
|
This should be pretty easily. There are two ways i can think of to do this: by using regex to check if the string is a valid number or trying to convert the last part of each line to a number.
Code:
pseudocode
import re
number = re.compile('^[\d\n\.]+$') #or other regex to match numbers.
for line in file('det.log'):
line = line.split('|')
if number.match(line[-1]):
...
do somthing with line.
...
Hope this helps, Mark. |
|
#3
|
|||
|
|||
|
How to the totals
Thanks |
|
#4
|
|||
|
|||
|
Another problem
if end of the is letters instead number it displays the letters too which i didn't want it to today, 12/12/12,|10.56|.monday.tot.col|ccc| thanks |
|
#5
|
||||
|
||||
|
Im pretty sure this has been covered before, not even a month ago maybe. Anyway, you need to convert the last part of the string to a number and if this is successful add it to the total. If not, just output the string.
Mark. |
|
#6
|
|||
|
|||
|
Yes mark we did go through this before the reason i come back to the same subject is converting the string to number and omitting the letters are not working properly anyway you metioned you can do it in 2 ways how do you do it with regex
Thanks |
|
#7
|
||||
|
||||
|
The code snippet above uses regex to check if the line contains a valid numer (the regex provided will work but it isnt very strict). After this regex line conferms that the string is what you want you still have to convert it to a float befor you can add up the total. What exactly is going wrong with the pogram?
Mark. |
|
#8
|
|||
|
|||
|
instead of using a regex you could try casting the last string to an int and catch any exceptions raised when you hit a string, ie:
Code:
total = 0
for line in file('det.log'):
line = line.split('|')
try:
num = int(line[-1])
total += num
print line
except ValueError:
pass
I'm not exactly sure what you want the output to be, but you get the idea. |
|
#9
|
||||
|
||||
|
Hey Rabbit,
Thats how we did it the first time, unfortunatly he says it isn't quite working out for him (not sure whats changed though). Aslo, these numbers are float() so you need to cast them as such. The output should simply be the total of all the floating point numbers: ignoring any that dont match/cast. Pyton also wants the end for each line to be outputted. Not a problem, just print line[-1] before or after the try block. Oh, and you'll also have to strip() any white space chars i.e. \n, from the end of he line or you're going to run into problems casting .Have fun, Mark. |
|
#10
|
|||
|
|||
|
Hi mark,rabbit ....etc
Thank you all for your valuablee help and support I have modified your code as follow Code:
import re
path='c:/det.log'
number = re.compile('^[\d\n\.]+$') #or other regex to match numbers.
for line in file(path):
line = line.split('|')
if number.match(line[-1]):
print ' '.join(line)
count=eval(line[-1])
total+=count
print '\t\t\t Total for %s = %0.2f' % (path,total)
this code works fine untill it finds the nubers instead of string i.e today, 12/12/12,|10.56|.monday.tot.col|5.00 the above line works fine but if it finds a line today, 12/12/12,|10.56|.monday.tot.col|567483 then it terminates I know i am converting "count=eval(line[-1]) to number but if the line[-1] is already a number then it drops the following error Code:
count=eval(line[-1])
File "<string>", line 1
000275269
^
SyntaxError: invalid token
Thanks |
|
#11
|
|||
|
|||
|
Hi all
I sorted all the above said problems and here is my code Code:
import sys, os, re
path='c:/det.log'
total=0.00
trim='|'
number=re.compile('^[\d\n\.]+$')
for line in file(path):
line=line.split(trim)
if line[-1].isdigit():
continue
if line[-1]=='.':
continue
if number.match(line[-1]):
print ' ' .join(line)
count=eval(line[-1])
total+=count
print '\t\t\t Total for %s = %0.2f' % (path,total)
Is teher a way i can compact my coding i know it's not right for example from line 4 to 6. i am checking for numbers and "." if they are found at line[-1] then to continue. Thanks all |
|
#12
|
|||
|
|||
|
Code:
count=eval(line[-1])
File "<string>", line 1
000275269
^
SyntaxError: invalid token
Numbers beginning with 0 are interpreted as octal - you can't have the digit '9' in an octal number. Your fix doesn't look like it would get around that. Quote:
Lines 4-6? Code:
trim='|'
number=re.compile('^[\d\n\.]+$')
for line in file(path):
?? Code:
if line[-1].isdigit(): if number.match(line[-1]): These are pretty much redundant - the regex will only match numbers, so it will do the same as the isdigit() test. Code:
if line[-1]=='.':
continue
This will continue if the last word in the string is '.' which isn't a number. Perhaps... Code:
import sys, os, re
path='c:/det.log'
total=0.00
number=re.compile('^[\d\n\.]+$')
for line in file(path):
# Remove line endings and split into chunks
line = line.strip().split('|')
# Take the last chunk, and remove starting zeros
temp = line[-1]
temp = temp.lstrip('0')
# If the chunk only contains numbers,
# Convert it to a number and add to the total
# or print the error message if conversion fails
if number.match(temp):
try:
total+=float(count)
except ValueError, err:
print err
print '\t\t\t Total for %s = %0.2f' % (path,total)
Would work? |
|
#13
|
|||
|
|||
|
Hi all
it's working Thanks for the help |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > line |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|