Save data to a csv file

Asked

Viewed 1,164 times

-3

I’m trying to save data to a csv file from fields:

def inserir():
    camp1 = str(ed1.get())
    camp2 = str(ed2.get())
    camp3 = str(ed3.get())

     with open("contatos.csv","w") as _file:
         _file.write("Nome;tel;endereço")
         _file.write(camp1)
         _file.write(camp2)
         _file.write(camp3)

return None
  • 1

    And what is the doubt? Edit the question and be clearer.

  • In the case the first line is leaving right, but the data that in the case the user typed by what in the case are stored in the variables camp, at most I can put one below the other in the column name.

  • Follow my suggestion and add this information in the question, click there on EDIT, so it is easier to understand the question without having to read here in the comments.

  • Have you tried using . writelines() ?

2 answers

0

I don’t remember if writelines works with variables, but basically that’s it.

 fh = open(“contatos.csv”,”w”) 
 linhas_de_texto = [camp1, camp2, camp3] 
 fh.writelines(linhas_de_texto) 
 fh.close()

You were there trying to write only one variable line by line

-1

I managed to resolve added

file.writelines(";".join([camp1,camp2,camp3]))

Browser other questions tagged

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