How can I write the generated data in different lines?

Asked

Viewed 43 times

0

Example: I want a number in one line, then in another line.

But all within the same generated text file.

from tkinter import*


janela = Tk()


#################Cria o arquivo##########################

def nomearq():
    arquivo = open(nameet.get(), 'w')
###########insere os dados no arquivo#############################

def esc():

  arquivo = open(nameet.get(), 'r') 
  conteudo = arquivo.readlines()
  conteudo.append(etmat.get()) 

  arquivo = open(nameet.get(), 'w')
  arquivo.writelines(conteudo)
  arquivo.close()
#############################################
mat = Label(janela, text="matricula:")
mat.grid(row=1, column=1)

etmat = Entry(janela,)
etmat.grid(row=1, column=2)

##############################################

namearq = Label(janela, text="nome do arquivo:")
namearq.grid(row=2, column=1)

nameet = Entry(janela,)
nameet.grid(row=2, column=2)

##############################################

Com = Button(text="Comfirmar", command=esc
             )
Com.grid(row=3,column=2)

##############################################

ger = Button(text="Gerar txt", command=nomearq)
ger.grid(row=4,column=2)




janela.geometry('300x300')
janela.mainloop

1 answer

1

The simplest way to do this line break is by using n:

def esc():
  arquivo = open(nameet.get(), 'r') 
  conteudo = arquivo.readlines()
  conteudo.append(etmat.get() + '\n') 

  arquivo = open(nameet.get(), 'w')
  arquivo.writelines(conteudo)
  arquivo.close()

Browser other questions tagged

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