I have a for loop which goes through a Dataframe and print some rows, maybe 2 or 3. I have written a code but it only write the last row to the text file. here is my code:
data = read_csv()
#how to change this line
data = data.set_index('Seat No')
print('Here is your ticket...')
x=read_file()
for seat_no in x:
row=data.loc[int(seat_no)]
ticket = open('ticket.txt', 'w')
old_stdout = sys.stdout
sys.stdout = ticket
ticket.writelines('''
{}-{} {}
{}
'''.format(row.Source, '%4s' % row.Destination, '%2s' % row.Departure, '%2s' % seat_no))
sys.stdout = old_stdout
ticket.close()
printed_ticket = open('ticket.txt').read()
print(printed_ticket)