[Tutor] Import multiple lines of text into a variable

Alan Gauld alan.gauld at btinternet.com
Tue Apr 12 09:27:15 CEST 2011


"Sean Carolan" <scarolan at gmail.com> wrote

> > The first two lines are redundant you only need the last one.
>
> I should have clarified, the "if line.startswith" part was used to
> break out of the previous for loop, which was used to import the
> other, shorter strings.

Thats fair enough if you are doing something with those 
shorter strings. But if not the whole loop is redundant, 
you only need the split. Your original post did not mention 
any processing of the earlier lines.

But even if you were you could still use the split() first 
then process the first element in a loop and assign the 
second element to your variable:

stuff, store = theFile.read().split('notes\n')
for line in stuff.split():
   # process thing

That way there is no need for a break test.

HTH.,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/





More information about the Tutor mailing list