[Tutor] RE: Tutor digest, Vol 1 #363 - 6 msgs

Carlos Angeles cangeles@mail.com
Mon, 17 Jul 2000 17:22:09 -0400 (EDT)

Message: 1
Reply-To: <djansen@pobox.com>
From: "David Jansen" <djansen@pobox.com>
To: <tutor@python.org>
Date: Sun, 16 Jul 2000 11:44:21 +0900
Subject: [Tutor] iterate problem

While I read tutorials I am playing around with what I have learned. I am
trying to write a simple script that removes the >>>'s from the beginning of
lines in a text file (for example  an email message that has been forwarded
a number of times). Removing the first > from the beginning of a line is no
problem but I seem to enter an endless loop when I attempt to iterate over
the line to get rid of second and third >'s if they exist.  The problem line
is obviously the while line in strip() but I can't figure out what is wrong
with it.

Any help greatly appreciated.

David Jansen

import string, sys

groups = []
alphas = string.letters + string.digits

file = raw_input("enter file: ")
inp = open(file, "r")

def strip(input):
____while (len(input) > 0) and (input[0] not in alphas):
________if input[0] == ">":
____________input = input[1:]
____return input

for line in inp.readlines():
____result = strip(line)
____print result

sample text file:

>Interesting Stuff.
> > >The citrus soda 7-UP was created in 1929; "7" was selected because the
> > >original containers were 7 ounces. "UP" indicated the direction of the
> > >bubbles.
> > >
> > > Mosquito repellents don't repel. They hide you. The spray blocks the
> > >mosquito's sensors so they don't know you're there.
> > >
> > > Dentists have recommended that a toothbrush be kept at least 6 feet


Have you tried making strip recursive?  Change it to look like this:

def strip(input):
____while (len(input) > 0)
________if input[0] == ">" or input [0]==" ":
____________input = strip(input[1:])
____return input

I didn't have a chance to test it with a file.  I just did a quick check in IDLE and it stripped off the leading "> > > >"'s that if fed it.  Let me know if that works.


FREE Personalized Email at Mail.com
Sign up at http://www.mail.com/?sr=signup