hey guys I need you help, I'm new comer in here but I hope this can help me.
I'm writing a code that can read the file (such a story or newspaper) then the identifythe story if that story has a itemY in it the it will print +1 otherwise -1, but it always the other way around i got...here I always get -1 even the story has "lol"

hope you gus can help me
here the code below so far

def my_f(modelfilename):
modelfile = open(modelfilename)
modelline = modelfile.readline()
itemY = ["lol", "young", "HoLa", "...", "Yum!", ":o)", "blokes", "!!!", "hello"]
itemO = ["old", "yes", "Mom", "serious", "blog"]

while len(modelline) > 0:
a = modelline.split()
if itemY in a:
print "+1 young"
else:
print "-1 old"
return a


print my_f('1032824.female.15.Student.Libra.xml')

thnx heaps

You are only checking the first record since you only do one readline(). Add some print statements after the while loop so you can see what you are looking through. If you use readlines(), the entire file will be read into a list

fp = open(modelfilename)
data = fp.readlines()
fp.close()
for modelline in data:
     print "looking for", itemY, "in", modelline
     a = modelline.split()
     if itemY in a:
          print "+1 young"
     else:
          print "-1 old"

Please use code tags to mainain proper Python indentations!

def my_f(modelfilename):
    modelfile = open(modelfilename)
    modelline_list = modelfile.readlines()
    modelfile.close()

    itemY = ["lol", "young", "HoLa", "...", "Yum!",
        ":o)", "blokes", "!!!", "hello"]
    itemO = ["old", "yes", "Mom", "serious", "blog"]

    for line in modelline_list:
        for itemy in itemY:
            if itemy in line:
                print "+1 young"
                # not sure if this is what you want?
                return line  
            else:
                print "-1 old"

My mistake. I missed that itemY is a list. Given that, you could also use sets as they are indexed. It may be also be more trouble than it's worth but is a good exercise. Also, if you don't want "the" to be found if looking at "theo" then you have to split and compare word to word. Assuming that you want to be case sensitive, that is "a" != "A"

## This Has NOT Been Tested
##modifying  Ene Uran's code
def my_f(modelfilename):
    modelfile = open(modelfilename)
    modelline_list = modelfile.readlines()
    modelfile.close()

    itemY = ["lol", "young", "HoLa", "...", "Yum!",
        ":o)", "blokes", "!!!", "hello"]
    itemY_set=set(itemY)

    for line in modelline_list:
         line_set=set(str(line).split())
         if line_set.intersection(itemY_set):
              print "+1 young"
              # not sure if this is what you want?
              return line  
         else:
              print "-1 old"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.