samedi 19 septembre 2020

Python class str comparison in if statement

I have to compare 2 data that are type <class 'str'> and are equal but the comparison always throws me that they are equal. Why?

this is the code:

def actualizacion(data, connection):
    f = open ('info.txt','r')
    new = f.read()
    print("this is : "+data+" - this is what is in the file: "+new)
    print("this is data type: "+ str(type(data)))
    print("this is new type: "+str(type(new)))

    if (data != new):
        msg="%!#|^$&%" + "new_data"+";¬"+new
        connection.sendall(msg.encode())
    f.close()

if gives True and this are the data:

this is : 8 - this is what is in the file: 8

this is data type: <class 'str'> this is new type: <class 'str'>

Aucun commentaire:

Enregistrer un commentaire