Сравнение строк python

Почему цикл не выводит единицу? Ведь строчки одинаковые Код:

f_read = open("C:\logs.txt", "r")
last_line = f_read.readlines()[-1]
f_read = open("C:\logs.txt", "r")
prelast_line = f_read.readlines()[-2]
print(prelast_line, end='')
print(last_line)
if prelast_line == last_line:
    print(1)
else:
    print(last_line)

Вывод:

6798: Нет открытых позиций
6798: Нет открытых позиций
6798: Нет открытых позиций

Process finished with exit code 0

Первые две строчки это принт, и он должен написать единицу, так как они одинаковые, но почему-то не так. В чем может быть проблема?

print(prelast_line == last_line)

Выводит false(


Ответы (1 шт):

Автор решения: arnold

Всё дело в символе переноса строки:

f_read = open("logs.txt", "r")
last_line = f_read.readlines()[-1]
last_line = last_line.rstrip('\n')
f_read = open("logs.txt", "r")
prelast_line = f_read.readlines()[-2]
prelast_line = prelast_line.rstrip('\n')
print("prelast_line: " + prelast_line)
print("last_line: " + last_line)
if prelast_line == last_line:
    print(1)
else:
    print(last_line)
→ Ссылка