Re: I can do it in sed...

2005-03-17 Thread Bengt Richter
On Thu, 17 Mar 2005 18:37:11 -0500, Kotlin Sam <[EMAIL PROTECTED]> wrote:

>Thanks to everyone who answered my two questions. I have only submitted 
>questions twice, and on both occasions the solutions were excellent, 
>and, I'm emarrassed to say, much simpler than I thought they would be.
>
>My next goal is to be able to help someone they way y'all have helped me.
>
Bravo. That's the spirit.
For even better appreciation of your future efforts, consider not top-posting 
;-)

>Thanks again,
>Lance
>
>Kent Johnson wrote:
>> Kotlin Sam wrote:
>> 
>>> Also, I frequently use something like s/^[A-Z]/~&/ to pre-pend a 
>>> tilde or some other string to the beginning of the matched string. I 
>>> know how to find the matched string, but I don't know how to change 
>>> the beginning of it while still keeping the matched part.
>> 
>> 
>> Something like
>> re.sub(r'^([A-Z])', r'~\1', target)
>> should do it.
>> 
>> Kent
>

Regards,
Bengt Richter
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I can do it in sed...

2005-03-17 Thread Kotlin Sam
Thanks to everyone who answered my two questions. I have only submitted 
questions twice, and on both occasions the solutions were excellent, 
and, I'm emarrassed to say, much simpler than I thought they would be.

My next goal is to be able to help someone they way y'all have helped me.
Thanks again,
Lance
Kent Johnson wrote:
Kotlin Sam wrote:
Also, I frequently use something like s/^[A-Z]/~&/ to pre-pend a 
tilde or some other string to the beginning of the matched string. I 
know how to find the matched string, but I don't know how to change 
the beginning of it while still keeping the matched part.

Something like
re.sub(r'^([A-Z])', r'~\1', target)
should do it.
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: I can do it in sed...

2005-03-17 Thread Kent Johnson
Kotlin Sam wrote:
Also, I frequently use something like s/^[A-Z]/~&/ to pre-pend a 
tilde or some other string to the beginning of the matched string. I 
know how to find the matched string, but I don't know how to change the 
beginning of it while still keeping the matched part.
Something like
re.sub(r'^([A-Z])', r'~\1', target)
should do it.
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: I can do it in sed...

2005-03-17 Thread TZOTZIOY
On Wed, 16 Mar 2005 19:06:40 -0600, rumours say that Terry Hancock
<[EMAIL PROTECTED]> might have written:



>You mean you have a text file and you want to find all the lines between
>a line starting with  "start" and one starting with "end".



>lines = open('myfile', 'r').readlines()
>printing = 0
>for line in lines:
>if line[:5]=='start': printing=1
>if line[:3]=='end':  printing=0
>if printing: print line

suggested order to mimic sed functionality:

>if line[:5]=='start': printing=1
>if printing: print line
>if line[:3]=='end':  printing=0

and perhaps it's better to use startswith than slicing.
-- 
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I can do it in sed...

2005-03-17 Thread Ville Vainio
> "John" == John Machin <[EMAIL PROTECTED]> writes:

John> You can get gnu Windows versions of awk sed and most other
John> suchlike goodies off the net ...

Yeah, google for 'unxutils'. Cygwin versions of these tools can be a
headache sometimes.

-- 
Ville Vainio   http://tinyurl.com/2prnb
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I can do it in sed...

2005-03-17 Thread Ville Vainio
> "Damjan" == Damjan  <[EMAIL PROTECTED]> writes:

Damjan> Or, much nicer
>> if line[:5]=='start': printing=1

Damjan> if line.startswith('start'): printing=1

>> if line[:3]=='end':  printing=0

Damjan> if line.endswith('end'):  printing=0

No, it's still line.startswith('end'), not endswith.

-- 
Ville Vainio   http://tinyurl.com/2prnb
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I can do it in sed...

2005-03-16 Thread Damjan
Or, much nicer

> if line[:5]=='start': printing=1

if line.startswith('start'): printing=1

> if line[:3]=='end':  printing=0

if line.endswith('end'):  printing=0

-- 
damjan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I can do it in sed...

2005-03-16 Thread John Machin

Kotlin Sam wrote:
> I have spent so much time using sed and awk that I think that way.
Now,
> when I have to do some Python things, I am having to break out of my
> sed-ness and awk-ness, and it is causing me problems. I'm trying.
Honest!
>
> Here are the two things that I'm trying to do:
>   In sed, I can print every line between ^start to ^end by using
> /^start/,/^end/p. It's quick, easy, and doesn't take much time. Is
there
>   a way to do this easily in Python?
>
>   Also, I frequently use something like s/^[A-Z]/~&/ to pre-pend a
tilde
> or some other string to the beginning of the matched string. I know
how
> to find the matched string, but I don't know how to change the
beginning
> of it while still keeping the matched part.
>
> If I were able to stay in the *nix environment for all my work, I
could
> do it with these tools and the beloved pipe(|), but that isn't my lot
in
> life. I would do it in Perl, but, frankly, it gives me headaches even

> looking at it.

You can get gnu Windows versions of awk sed and most other suchlike
goodies off the net ...

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I can do it in sed...

2005-03-16 Thread Terry Hancock
On Wednesday 16 March 2005 06:01 pm, Kotlin Sam wrote:
> Here are the two things that I'm trying to do:
>   In sed, I can print every line between ^start to ^end by using 
> /^start/,/^end/p. It's quick, easy, and doesn't take much time. Is there 
>   a way to do this easily in Python?

You mean you have a text file and you want to find all the lines between
a line starting with  "start" and one starting with "end".

REs are not your best method here, just do something like this:

lines = open('myfile', 'r').readlines()
printing = 0
for line in lines:
if line[:5]=='start': printing=1
if line[:3]=='end':  printing=0
if printing: print line

Or something like that.  I'm sure there are cleverer ways, but
that should do what you ask for.

Cheers,
Terry

-- 
--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com

-- 
http://mail.python.org/mailman/listinfo/python-list


I can do it in sed...

2005-03-16 Thread Kotlin Sam
I have spent so much time using sed and awk that I think that way. Now, 
when I have to do some Python things, I am having to break out of my 
sed-ness and awk-ness, and it is causing me problems. I'm trying. Honest!

Here are the two things that I'm trying to do:
	In sed, I can print every line between ^start to ^end by using 
/^start/,/^end/p. It's quick, easy, and doesn't take much time. Is there 
 a way to do this easily in Python?

	Also, I frequently use something like s/^[A-Z]/~&/ to pre-pend a tilde 
or some other string to the beginning of the matched string. I know how 
to find the matched string, but I don't know how to change the beginning 
of it while still keeping the matched part.

If I were able to stay in the *nix environment for all my work, I could 
do it with these tools and the beloved pipe(|), but that isn't my lot in 
life. I would do it in Perl, but, frankly, it gives me headaches even 
looking at it.

Any ideas?
Thanks, Lance
--
http://mail.python.org/mailman/listinfo/python-list