Python Forum
Multiple entries into a csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple entries into a csv file
#2
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.
Reply


Messages In This Thread
Multiple entries into a csv file - by SatansClaw - Jun-11-2019, 10:24 AM
RE: Multiple entries into a csv file - by stullis - Jun-13-2019, 07:06 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020