I am trying to read the last 3 lines from the file, but only the last one is given. I do not understand what the catch is. Thank you in advance!

with open("c.txt", 'r') as f: last3 = deque(f, 3) for line in last3: co.append(line.strip().split(',')) for pair in co: x,y,z = pair[0],pair[2] sommething = x,y return render_template("page.html", something=something) f.close() 
  • The catch is that you constantly rewrite the sommething variable and only the last value remains in it. - user194374

2 answers 2

Try this

 with open("c.txt", 'r') as f: last3 = deque(f, 3) for line in last3: co.append(line.strip().split(',')) sommething = tuple((pair[0],pair[2]) for pair in co) return render_template("page.html", something=something) f.close() 
  • thank. This option works. just how can I display a list without commas. Instead of ('Маша', 'Иванова') , so that it would be Маша Иванова on the page. Because in my version there were no commas and brackets when displayed. - Alex
  • '' .join (('Masha', 'Ivanova')) - Dmitry Erohin
 def get_something(file): with open(file) as f: last3 = deque(f, 3) for line in last3: co.append(line.strip().split(',')) for pair in co: x,y,z = pair[0],pair[2] yield x,y render_template("page.html", something=list(get_something('c.txt')))