OK, so I'm trying to get input from the user and store it in a csv file (I'm using microsoft excel) under the headers of 'username' and 'password'. I've successfully done this using the DictWriter command, but when I try again with different data, it just replaces the previous data. In short, I want to find a way to put the new data in a new row, under the same headers.
my code looks like this:
name = input("enter you name")
age = input("enter your age in years")
username = name[:3] + age
print("your unique username is", username)
while True:
password = input("enter a password over 6 characters")
count = 0
for letter in password:
count = count + 1
if count < 6:
print("password too short, please enter another")
else:
break
import csv
with open('usernames.csv', 'w', newline='') as csvfile:
fieldnames = ['username', 'password']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'username': username, 'password': password})
when i run it, it works:
============ RESTART: /Users/Hesta/Desktop/python csv/username.py ============
enter you namejohn smith
enter your age in years19
your unique username is joh19
enter a password over 6 characterslongpassword
>>>
This data gets sent to a csv file under the headers 'username' and 'password'. However, if I run the code again, and enter different data, it works again, but completely replaces the previously entered data. How do I make it so that the new data is stored on a different row?