简体   繁体   中英

'_csv.writer' object is not iterable. how to append to a csv file

start of the code and if the user choses y works fine just if the user choses n

import time
import csv

def login():
    print("Welcome...")
    welcome = raw_input("Do you have an acount? y/n")
    if welcome == "y":
        with open("user.csv",'r') as file:
            username=raw_input("Enter username")
            password=raw_input("Enter password")
            fileReader=csv.reader(file)
            for row in fileReader:
                if username==row[0] and password==row[1]:
                    print("access granted")
                    return username
                else:
                    print("denied")
                    login()


    elif welcome == "n":
        with open("user.csv",'a') as file:
            username=raw_input("Enter username")
            password=raw_input("Enter password")
            csv_writer = csv.writer(csvfile)
            for row in csv_writer:
                username==row[0] and password==row[1]

    else:
        print("denied")
        login()

csv file: miles,miesgw bob,phone simon,smells

problem occurs when users input is n unsure in how to append to the csv file and save.

Try:

with open("user.csv",'a') as file:
    username=raw_input("Enter username")
    password=raw_input("Enter password")
    csv_writer = csv.writer(file)
    csv_writer.writerow([username, password])

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