user1469051
user1469051

Reputation: 57

Python: readline() == string literal never returns True

Edit: I made a simple error 'testing 1,2,3+\n' should be 'testing 1,2,3'+'\n' or 'testing 1,2,3\n'

I'm trying to conditionally read text from a file. I can't figure out a conditional that will return True when I compare my_file.readline() to some

my_file.readline() == 'string literal' always returns false.

with open('text.txt','w') as my_file:
    my_file.write('testing 1,2,3'+'\n'+'filter me'+'\n')  #writing arbitrary text to my  file


with open('text.txt','r') as my_file:
    str_from_file = my_file.readline()        # first line should be 'testing 1,2,3'
    print str_from_file == 'testing 1,2,3+\n' #both print False
    print str_from_file == 'testing 1,2,3'    #what string literal would print true?
    print str_from_file

Obviously, I'm a huge nub with python & coding. This is my 5th day with Python.

Upvotes: 1

Views: 966

Answers (1)

mgilson
mgilson

Reputation: 309929

you're comparing the strings:

'testing 1,2,3'+'\n'  # 'testing 1,2,3\n'

and

'testing 1,2,3+\n'

Notice how the second string has an additional '+' character stuck in there.

Upvotes: 3

Related Questions