There are probably "return" characters at the end of each "line" from the
"grocery_list".
Try using the String method "line.strip()".
Or "grocery_list.read().splitlines()"
----- Original Message -----
From: "Eric Hamiter" <ehami...@gmail.com>
To: <tutor@python.org>
Sent: Wednesday, July 14, 2010 8:46 AM
Subject: [Tutor] Searching a text file's contents and comparing them to
alist
Hi all,
New programmer here. This is what I want to do:
1. Open an existing text file named "grocery_list.txt", which has one
item per line, like so:
butter
juice
bread
asparagus
magazines
ice cream
2. ...and search for these items in a pre-defined list.
But I can't seem to get this working. Right now after trying the
following:
aisle_one = ["chips", "bread", "pretzels", "magazines"]
grocery_list = open("grocery_list.txt", "r")
for line in grocery_list.readlines():
if line in aisle_one:
print "success"
else:
print "no joy"
grocery_list.close()
I get this:
no joy
no joy
no joy
no joy
no joy
no joy
when I'm expecting this:
no joy
no joy
success
no joy
success
no joy
Am I close? This seems like it should work but I'm obviously missing
something.
Thanks,
Eric
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor