Re: [Tutor] Read file line by line

2005-01-26 Thread Alan Gauld
 1. Why does the assignment-and-test in one line not allowed in
Python?
 For example, while ((content = fd.readline()) != ):

Because Guido didn't write it that way? ;-)

And that may have been because it is such a common source of bugs.
So common in fact that many compilers now offer to emit a warning
when they detect it...just in case you meant to use ==...

 2. I know Perl is different, but there's just no equivalent of while
 ($line = A_FILE) { } ?

Take a look at the fileinput module
and of course

for line in fd:
   #do something

works too.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld

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


Re: [Tutor] Read file line by line

2005-01-25 Thread Danny Yoo


On Tue, 25 Jan 2005, Gilbert Tsang wrote:

 Hey you Python coders out there:

 Being a Python newbie, I have this question while trying to write a
 script to process lines from a text file line-by-line:

 #!/usr/bin/python
 fd = open( test.txt )
 content = fd.readline()
 while (content !=  ):
 content.replace( \n,  )
 # process content
 content = fd.readline()

 1. Why does the assignment-and-test in one line not allowed in Python?
 For example, while ((content = fd.readline()) != ):


Hi Gilbert, welcome aboard!

Python's design is to make statements like assignment stand out in the
source code.  This is different from Perl, C, and several other languages,
but I think it's the right thing in Python's case.  By making it a
statement, we can visually scan by eye for assignments with ease.


There's nothing that really technically prevents us from doing an
assignment as an expression, but Python's language designer decided that
it encouraged a style of programming that made code harder to maintain.
By making it a statement, it removes the possiblity of making a mistake
like:

###
if ((ch = getch()) = 'q') { ... }
###



There are workarounds that try to reintroduce assignment as an expression:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/202234

but we strongly recommend you don't use it.  *grin*




 2. I know Perl is different, but there's just no equivalent of while
 ($line = A_FILE) { } ?

Python's 'for' loop has built-in knowledge about iterable objects, and
that includes files.  Try using:

for line in file:
...

which should do the trick.


Hope this helps!

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


Re: [Tutor] Read file line by line

2005-01-25 Thread Danny Yoo


 There's nothing that really technically prevents us from doing an
 assignment as an expression, but Python's language designer decided that
 it encouraged a style of programming that made code harder to maintain.
 By making it a statement, it removes the possiblity of making a mistake
 like:

 ###
 if ((ch = getch()) = 'q') { ... }
 ###

hmmm.  This doesn't compile.  Never mind, I screwed up.  *grin*


But the Python FAQ does have an entry about this topic, if you're
interested:

http://python.org/doc/faq/general.html#why-can-t-i-use-an-assignment-in-an-expression

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