because when you loop over open(...) is the same as looping over open
(...).readlines() and readlines() reads everything including newlines.
Try replace:
if item == var:
with
if item.strip() == var:
Massimo
On Nov 28, 2008, at 9:47 PM, [EMAIL PROTECTED] wrote:
Hi All,
I dont understand why the following code never finds "tree".
I could not find the answer in the Python tutorials.
Here is the code, test43.in, and runtime:
#!/usr/bin/python
fname = open("test43.in")
var = 'tree'
for item in fname:
print "item: ", item,
if item == var:
print "found tree: ", item,
[EMAIL PROTECTED] work]$
[EMAIL PROTECTED] work]$
[EMAIL PROTECTED] work]$ cat test43.in
car
tree
house
pool
dog
cat
wax
candy bar
[EMAIL PROTECTED] work]$ python test43.py
item: car
item: tree
item: house
item: pool
item: dog
item: cat
item: wax
item: candy bar
Thanks, [EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-announce-list
Support the Python Software Foundation:
http://www.python.org/psf/donations.html
--
http://mail.python.org/mailman/listinfo/python-list