My question is :

x = "you love me"

Inside 1234.txt file:

i love you
you love me
we love you

If i wan IF x == "you love me" from 1234.txt, THEN x is true.

how to do it? any one can help?

Member Avatar for sravan953

My question is :

x = "you love me"

Inside 1234.txt file:

i love you
you love me
we love you

If i wan IF x == "you love me" from 1234.txt, THEN x is true.

how to do it? any one can help?

Try:

log=open('1234txt','r')
log_read=log.readlines()

x=""
for a in log_read:
    if (a=='you love me'):
        x="True"
Member Avatar for leegeorg07

that would work, but couldnt you use:

log=open('1234txt','r')
log_read=log.readlines()

x="you love me"
if x in log_read:
    return True

i haven't tested this but it should work up to python 3/3.1
i dont know if it still supports the 'in' in it but its worth a shot
i use it all the time with 25

If you are specifically looking for the text line:
'you love me'
then sravan953 code works best.

leegeorg07 code using 'in' would also give you a positive with a line like:
'you love me not'
which may be misleading.

On the other hand 'in' would fetch:
'you love me!'
which would be a shame to miss!

Or: if it doesn't neccessarily take up the entire line:

my_file = open('1234.txt','r')
whole_file = my_file.read()
my_file.close()

x = 'you love me'


def find_x_in_string(x,whole_file)
    index = 0
    end = len(x) - 1
    return_list = []
    while 1:
        try:
            my_str = whole_file[index:end]
            if my_str == x:
                return_list.append(my_str)
            index += 1
            end += 1
        except IndexError:
            break
    return return_list

return_list = find_x_in_string(x,whole_file)

return_list would have all occurrences of x.

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.