4
I’d like to go to my file that contains the following
['teste1', '27-12-18', '12/5/2015', 'Aberta']
['teste2', '27-12-18', '6/7/7', 'Aberta']
['teste3', '27-12-18', '12/5/6', 'Fechada']
and only show if the 3 field is Open I have the following code;
File:
def annexsentence():
data_inicio=input("Nome da Tarefa :")
data_final=input("Data Final :") #placeholder ano-mes-dia
data_atual = datetime.datetime.now().strftime("%d-%m-%y")
estado="Aberta"
lista=[data_inicio,str(data_atual),str(data_final),estado]
annexsentence=open("ficheiro.txt","a")
annexsentence.write(str(lista)+"\n")
annexsentence.close()
annexsentence()
File reading:
def mostrar():
#ler linha a linha
with open("ficheiro.txt") as arquivo:
for linha in arquivo:
if linha[3]=="Aberta":
print (linha)
else:
print("ola")
mostrar()
Can someone explain to me what I should do to fix?
If you do print(linha[3])
back and test and stick linha[0]
appears [
You who generated the file?
– Woss
yes, I have more code but I think the problem is here
– Pedro Pinheiro
The question is how Python will interpret the contents of the file. The way you wrote the file is not such a trivial way to recover the structure, so probably the ideal solution is to redo the writing logic of the file. You can use JSON or serialize the object, for example.
– Woss
but in this case I really have to use python is for a course project, but what is confusing me and just appear 1 character and not everything
– Pedro Pinheiro
The suggestion is to still continue using Python, but writing the file in a way that is simpler to recover the data. Please edit the question and add the code you use to write to the file.
– Woss