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

Emile van Sebille emile at fenx.com
Wed Jul 14 18:55:29 CEST 2010


On 7/14/2010 9:22 AM Eric Hamiter said...
> Fantastic! I have this, which now works. Is there a better place to put
> string.strip?

I might make the changes below, but there's nothing wrong with the way 
you've got it.

>
> aisle_one = ["chips", "bread", "pretzels", "magazines"]
>
> grocery_list = open("grocery_list.txt", "r")

grocery_list= [ item.strip()
    for item in open("grocery_list.txt", "r").readlines() ]

> for line in grocery_list.readlines():

for item in grocery_list:

>      if line.strip() in aisle_one:

     if item in aisle_one:

>         print "success! i found %s" % line
>      else:
>         print "no joy matching %s" % line
> grocery_list.close()
>
>



More information about the Tutor mailing list