[Tutor] Searching a text file's contents and comparing them to a list

Eric Hamiter ehamiter at gmail.com
Wed Jul 14 18:22:33 CEST 2010


Fantastic! I have this, which now works. Is there a better place to put
string.strip?

aisle_one = ["chips", "bread", "pretzels", "magazines"]

grocery_list = open("grocery_list.txt", "r")
for line in grocery_list.readlines():
    if line.strip() in aisle_one:
       print "success! i found %s" % line
    else:
       print "no joy matching %s" % line
grocery_list.close()


Thanks Emile.


On Wed, Jul 14, 2010 at 11:02 AM, Emile van Sebille <emile at fenx.com> wrote:


> Try adding some debugging code here, maybe changing the print to:
>          print "no joy matching %s" % line
>
> Then read up on readlines and string.strip.
>
> HTH,
>
> Emile
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100714/ab85039c/attachment.html>


More information about the Tutor mailing list