Arq.readlines python / Tkinter loop

Asked

Viewed 64 times

1

I’m new in Python and I’m doing a report and I’m not getting a loop to read a txt file that I have.

the data from the txt file are:
('001','0:59:30.0','1','3.00')
('002','0:59:30.0','2','4.00')
('003','0:59:30.0','3','5.00')
('004','0:59:30.0','4','6.00')
('005','0:59:30.0','5','7.00')
I want the loop to end only when the last line is written, I put the first 5 items on the list.

The format I want to leave is in a list relattxt as shown below

relattxt = [('001', '0:59:30.00', '1', '3.00'),
            ('002', '0:59:30.00', '2', '4.00'),
            ('003', '0:59:30.00', '3', '5.00'),
            ('004', '0:59:30.00', '4', '6.00'),
            ('005', '0:59:30.00', '5', '7.00')]

I know I need to use the command

arq = open(f'relatorio.txt', 'r')
texto = arq.readlines()

I can also change the format of the txt data to make it easier please help me
thank you

1 answer

0


With Ast.literal_eval:

from ast import literal_eval

with open("relatorio.txt") as arquivo:
    relattxt = [literal_eval(linha) for linha in arquivo]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.