Re: [Tutor] Import multiple lines of text into a variable

2011-04-12 Thread Steven D'Aprano

Sean Carolan wrote:

if line.startswith('notes'):
  break
notes = open('myfile','r').read().split(notes:\n')[1]

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.



Just for reference, "import" has special meaning in Python, and you hurt 
my brain by using it as a synonym for "read".


For what it's worth, here's my solution. Rather than use the funky new 
"open files are iterable" feature, go back to the old-style way of 
reading line by line:


# untested
fp = open("myfile.txt")
for while True:
line = fp.readline()  # read one line
if not line:
# nothing left to read
break
if "ham" in line:
process_ham(line)  # Mmmm, processed ham...
if "spam" in line:
process_spam(line)
if line.startswith("notes"):
notes = fp.read()  # reads the rest of the file
fp.close()


Note that it is okay to mix calls to read() and readline(), but it is 
NOT okay to mix iteration over a file with calls to read() or readline().





--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Import multiple lines of text into a variable

2011-04-12 Thread Alan Gauld


"Sean Carolan"  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/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Import multiple lines of text into a variable

2011-04-11 Thread Sean Carolan
>> if line.startswith('notes'):
>>   break
>> notes = open('myfile','r').read().split(notes:\n')[1]
>
> 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.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Import multiple lines of text into a variable

2011-04-11 Thread Alan Gauld


"Sean Carolan"  wrote

I ended up doing this, but please reply if you have a more elegant 
solution:


if line.startswith('notes'):
   break
notes = open('myfile','r').read().split(notes:\n')[1]


The first two lines are redundant you only need the last one.

HTH,

Alan G. 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Import multiple lines of text into a variable

2011-04-11 Thread bob gailer

On 4/11/2011 5:14 PM, Sean Carolan wrote:

So right now my code looks something like this:

for line in open('myfile','r'):
  if line.startswith('notes'):
  ## Assign rest of file to variable

Is there an easy way to do this?  Or do I need to read the entire file
as a string first and carve it up from there instead?

I ended up doing this, but please reply if you have a more elegant solution:

if line.startswith('notes'):
break
notes = open('myfile','r').read().split(notes:\n')[1]


Seems like an elegant solution to me, as long as the file fits available 
memory. There will be 2 copies of the file after the split.


Another way:

textFile = open('myfile','r')
for line in textFile:
 if line.startswith('notes'):
  notes = textFile.read()

--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Import multiple lines of text into a variable

2011-04-11 Thread Sean Carolan
> So right now my code looks something like this:
>
> for line in open('myfile','r'):
>  if line.startswith('notes'):
>      ## Assign rest of file to variable
>
> Is there an easy way to do this?  Or do I need to read the entire file
> as a string first and carve it up from there instead?

I ended up doing this, but please reply if you have a more elegant solution:

if line.startswith('notes'):
   break
notes = open('myfile','r').read().split(notes:\n')[1]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor