Re: [Tutor] trouble with re

2006-05-08 Thread Ertl, John
Kent,

Thanks for the nock on the head,  that has bitten me before.  Taking out the
spaces worked great.

Thanks again,

John Ertl 

 -Original Message-
From:   [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]  On
Behalf Of Kent Johnson
Sent:   Monday, May 08, 2006 10:53 AM
Cc: tutor@python.org
Subject:Re: [Tutor] trouble with re

Ertl, John wrote:
> I have a file with 10,000 + lines and it has a coma delimited string on
each
> line.
> 
> The file should look like:
> 
> DFRE,ship name,1234567
> FGDE,ship 2,
> ,sdfsf
> 
> The ,sdfsf  line is bad data
> 
> p = re.compile('\d{7}$ | [,]$')   # this is the line that I can not get
> correct I an trying to find lines that end in a comma or 7 digits

Spaces are significant in regular expressions unless you compile them 
with the re.VERBOSE flag. Also you don't need to make a group for a 
single character. Try
p = re.compile('\d{7}$|,$')
or maybe
p = re.compile('(\d{7}|,)$')

Actually since the seven digits are preceded by the comma you could just 
make the digits optional:
p = re.compile(',(\d{7})?$')

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] trouble with re

2006-05-08 Thread Kent Johnson
Ertl, John wrote:
> I have a file with 10,000 + lines and it has a coma delimited string on each
> line.
> 
> The file should look like:
> 
> DFRE,ship name,1234567
> FGDE,ship 2,
> ,sdfsf
> 
> The ,sdfsf  line is bad data
> 
> p = re.compile('\d{7}$ | [,]$')   # this is the line that I can not get
> correct I an trying to find lines that end in a comma or 7 digits

Spaces are significant in regular expressions unless you compile them 
with the re.VERBOSE flag. Also you don't need to make a group for a 
single character. Try
p = re.compile('\d{7}$|,$')
or maybe
p = re.compile('(\d{7}|,)$')

Actually since the seven digits are preceded by the comma you could just 
make the digits optional:
p = re.compile(',(\d{7})?$')

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] trouble with re

2006-05-08 Thread Ertl, John
I have a file with 10,000 + lines and it has a coma delimited string on each
line.

The file should look like:

DFRE,ship name,1234567
FGDE,ship 2,
,sdfsf

The ,sdfsf  line is bad data


Some of the lines are messed up...I want to find all lines that do not end
in a comma or seven digits and do some work on them.  I can do the search
for just the last seven digits but I can not do the seven digits or the
comma at the end in the same search.

Any ideas


import re
import sys
import os

p = re.compile('\d{7}$ | [,]$')   # this is the line that I can not get
correct I an trying to find lines that end in a comma or 7 digits
newFile = open("newFile.txt",'w')
oldFile = open("shipData.txt",'r')

for line in oldFile:
if p.search(line):
   newFile.write(line)
else:
   newFile.write("*BAD DATA " + line)

newFile.close()
oldFile.close() 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor