Python Forum

Full Version: Multiple entries into a csv file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The following code allows me to add only 2 sets of data to the csv file. The second bit of code continually overwrites the last 3 entries that I make. Please can anyone help me with this problem, all I wish to do is to add multiple entries without anything being overwritten?

I have copied the code below;

import csv

with open('users2.csv','a') as f:# 'a' will add to the existing csv file.
    #f.write()#f.write('user,pword,day')
    x=input("User name? ")
    y=input("Password? ")
    z=input("Day? ")
with open('users2.csv','a',newline='') as f:
    writer=open('users2.csv')
    writer=csv.writer(f)
    writer.writerow([])
    writer.writerow([x,y,z])
    response=input("Would you like another entry; Yes or No? ")
    while response == 'Yes':
        a=input("User name? ")
        b=input("Password? ")
        c=input("Day? ")
        response=input("Would you like another entry; Yes or No? ")             
    else:
            response=input("Return to end...")         
with open('users2.csv','a') as f:
    writer=csv.writer(f)
    writer.writerow([a,b,c])
There are a few things to change to make this code more streamlined and Pythonic. First, there are too many with statements and open() calls. Since you're using the same file for all of them, we can use one with and one open():

import csv

with open('users2.csv','a',newline='') as f: # 'a' will add to the existing csv file.
    x = input("User name? ")
    y = input("Password? ")
    z = input("Day? ")
    writer = csv.writer(f)
    writer.writerow([])
    writer.writerow([x,y,z])
    response=input("Would you like another entry; Yes or No? ")
    while response == 'Yes':
        a=input("User name? ")
        b=input("Password? ")
        c=input("Day? ")
        response=input("Would you like another entry; Yes or No? ")             
    else:
        response=input("Return to end...")         
    writer=csv.writer(f)
    writer.writerow([a,b,c])
Now that everything's been moved around, a problem becomes evident. There are a number of identical lines and the script gives the user an option to make more entries; this is a perfect case for a loop:

import csv

with open('users2.csv','a',newline='') as f: # 'a' will add to the existing csv file.
    writer = csv.writer(f)
    writer.writerow([])
    while True:
        x = input("User name? ")
        y = input("Password? ")
        z = input("Day? ")
        writer.writerow([x,y,z])
        response = input("Would you like another entry; Yes or No? ")
        
        if response != 'Yes':
            break
Instantiating a csv.writer() is performed once before the loop starts and then the blank row (or header if you replace it) is written. The loop begins with the three inputs and writes them to the file.

When checking the response, check for it being not equal to the affirmative answer. The alternative is not useful because it would merely continue the loop which will happen regardless. A check at the end of a loop is only valuable when it contradicts or changes the expectation.

One more thing to improve this: when checking typed responses, use a tuple and str.lower() to allow for various inputs:

import csv

with open('users2.csv','a',newline='') as f: # 'a' will add to the existing csv file.
    writer = csv.writer(f)
    writer.writerow([])
    while True:
        x = input("User name? ")
        y = input("Password? ")
        z = input("Day? ")
        writer.writerow([x,y,z])
        response=input("Would you like another entry; Yes or No? ")
        
        if response.lower()) not in ('yes', 'y', 'yeah', 'totally'):
            break
There shouldn't be any issues now.