简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM