Lakhan Mankani
Lakhan Mankani

Reputation: 69

Write to next line python csv

I am working on a project which I figured would need to use something like a CSV document for storing data. But I ran into a problem, I need python to write the variable into the next line every time instead of the first line (this is overwriting the existing data in that column). Could anyone please help me to solve my problem?

Here is my code:

import csv
tag=input('Enter label for the new profile: ')
pwd=input('Type password for the new profile: ')
b=open('database.csv', 'w',)
a=csv.writer(b)
a.writerow([tag,pwd])
b.close()

Thanks in advance.

Upvotes: 0

Views: 886

Answers (1)

idjaw
idjaw

Reputation: 26600

You want to use 'append' mode for your write.

Change your 'w' mode in your open 'a'.

Change this:

b=open('database.csv', 'w',)

to this:

b=open('database.csv', 'a',)

Read the documentation here on the different modes

'w' for only writing (an existing file with the same name will be erased), and 'a' opens the file for appending; any data written to the file is automatically added to the end.

Upvotes: 2

Related Questions