如何在python登录脚本中去除csvreader的格式

时间:2017-10-26 16:46:38

标签: python csv login formatting

您好我正在尝试为作业创建一个登录脚本我几乎在那里,但我的csvreader输出有奇怪的格式,例如它显示字符串['密码']而不仅仅是密码,这是我需要的。如果可能的话,我想避免使用正则表达式。

import csv

def createAccount():
    userCreated = False
    print("CREATE AN ACCOUNT!")
    while not userCreated:
        username1 = input("Enter Username:")
        password1 = input("Enter Password:")
        if username1 and password1 != "":            
            userCreated = True
            print("ACCOUNT CREATED!")
            loginDetails1 = username1+password1
            with open("test234.csv", 'w') as output:
              writer = csv.writer(output)
              writer.writerow([loginDetails1])
        if userCreated is not True:
            print("ENTER ALL DETAILS!")


def login():
    loggedIn = False
    while not loggedIn:
        username = input("Username:")
        password = input("Password:")
        loginDetails = username+password
        with open("test234.csv", "r") as csv_file:
                csv_reader = csv.reader(csv_file)
                for line in csv_reader:
                    if loginDetails == line:
                        loggedIn = True
                        print("Logged in")
                if loggedIn is not True:
                    print("Wrong username or password")
                    print(line)
                    print(loginDetails)


createAccount()
login()

输出看起来像enter image description here

1 个答案:

答案 0 :(得分:0)

您的密码位于line[0]。阅读器根据分隔符和引号将每一行分成不同的字段,因此每一行都是一个字符串列表