name = "name" email = "email" naslov = "RONKOVA ULICA" telefonska = "02 832 07 60" ulica ,postna = naslov.split(",") naslov.strip() rows = [[name, email, ulica, naslov, telefonska]] # Write to csv file fields = ["Ime", "Mail", "Ulica", "Poštna Številka", "Telefonska"] np.savetxt("output.csv", rows, delimiter =", ", fmt ='% s') `name = "name"
this is the code for writing to a CSV file but I have a problem that every time I write to the file it deletes everything it was in it before. Is there a way to write in a CSV file even if something is in it, but writing in a new line?. So the output is something like name,mail,ulica,postna,telefonska, something,something else, Ronkova, else,
And if you are wondering what words like ulica mean. They are in my language(slovenia) ulica means street, poštna številka means post number and telefonska means phone number.
I recommend that you directly write to the file instead of using the numpy api, since
savetxt (like other
np.save... methods) is supposed to generate a new file every time. You can achieve what you want with:
name = "name" email = "email" naslov = "RONKOVA ULICA" telefonska = "02 832 07 60" ulica ,postna = naslov.split() naslov.strip() rows = [[name, email, ulica, naslov, telefonska]] with open("output.csv", "a") as outfile: for row in rows: print(", ".join(map(str, row)), file=outfile)
"a" flag in the
open method means that you are appending to the output file.
Executing the code above two times left my
output.csv file with this content:
name, email, RONKOVA, RONKOVA ULICA, 02 832 07 60 name, email, RONKOVA, RONKOVA ULICA, 02 832 07 60