Skip to main content
 首页 » 编程设计

python之从文件中读取行对

2025年05月04日10jpfss

我希望它读取第 0 行和第 1 行,0 代表用户名,1 代表密码,如果它没有找到它,它会在行号上加 2,所以它读取 2 代表用户名,然后 3 代表密码,然后等等。

我的文件看起来像这样:

daniel 
password 
user 
password 
user 
password 
etc user 
etc password 
 
def PupilLogin(): 
 
    print "*********************************" 
    print "* Welcome to Spelling Bee Pupil *" 
    print "*********************************" 
    print "Enter your details below..\n" 
    strID = raw_input("Username : ") 
    strPassWd = getpass.getpass("Password : ") 
    with open('studentsusers.csv', 'rb') as csvfile: 
        spamreader = csv.reader(csvfile, delimiter=',') 
        username = 0 
        password = 1 
        for row in spamreader: 
            strLoginID = row[username] 
            strLoginPasswd = row[password] 
            while strLoginID != "": 
                if strID == strLoginID and strLoginPasswd == strPassWd: 
                    print strID, "Logged In" 
                    PupilMenu() 
                    strContinue = sys.stdin.readline() 
                    return 
                else: 
                    username += 2 
                    password += 2 
    #if we get here there is no such login and id 
    print "Login ID or password incorrect, press enter to continue" 
    strContinue = sys.stdin 

我认为这个问题与我没有使用整数这一事实有关,但我实际上没有任何线索我认为我在这里很愚蠢,老实说只是希望得到任何帮助。

请您参考如下方法:

我认为问题在于您的代码需要一个以逗号分隔的 csv 文件,该文件看起来像这样:

daniel, password 
user, password 

因此每一行都有一个用户名 (row[0]) 和一个密码 (row[1]),以逗号分隔。您可以使用 username, password = row 的形式轻松地将这些分配给变量。

代码的 for row in spamreader 部分遍历每一行,因此在第一次迭代中,row 等于 ["daniel", "password"]等

我会更改您的 .csv 文件以适应该格式,然后像这样执行代码的相关部分:

strID = raw_input("Username : ") 
strPassWd = getpass.getpass("Password : ") 
 
with open('studentsusers.csv', 'rb') as csvfile: 
    spamreader = csv.reader(csvfile, delimiter=',') 
 
    for row in spamreader: 
        strLoginID, strLoginPasswd = row 
 
        while strLoginID != "": 
            if strID == strLoginID and strLoginPasswd == strPassWd: 
                print strID, "Logged In" 
                PupilMenu() 
                strContinue = sys.stdin.readline() 
                return 
            else: 
                break   # goes on to the next row in spamreader 
 
#if we get here there is no such login and id 
print "Login ID or password incorrect, press enter to continue" 
strContinue = sys.stdin 

编辑:我应该提一下,当您不再需要文件资源时,释放文件资源通常是个好主意。在此示例中,一旦用户登录,您就不再需要您的 csv 文件,因此我会考虑退出 PupilMenu() 调用,并在 文件是关闭(即在 with block 之后)。考虑一下:

strID = raw_input("Username : ") 
strPassWd = getpass.getpass("Password : ") 
 
loggedIn = False 
 
with open('studentsusers.csv', 'rb') as csvfile: 
    spamreader = csv.reader(csvfile, delimiter=',') 
 
    for row in spamreader: 
        strLoginID, strLoginPasswd = row 
 
        while strLoginID != "": 
            if strID == strLoginID and strLoginPasswd == strPassWd: 
                print strID, "Logged In" 
                loggedIn = True 
                return 
            else: 
                break   # goes on to the next row in spamreader 
 
if loggedIn: 
    PupilMenu() 
    strContinue = sys.stdin.readline() 
else: 
    print "Login ID or password incorrect, press enter to continue" 
    strContinue = sys.stdin 

编辑 2:为了好玩,我稍微重构了您的代码。我认为这更容易阅读/理解:

def Welcome(): 
    print "*********************************" 
    print "* Welcome to Spelling Bee Pupil *" 
    print "*********************************" 
    print "Enter your details below..\n" 
 
    strID = raw_input("Username : ") 
    strPassWd = getpass.getpass("Password : ") 
 
    if loggedIn(strID, strPassWd): 
        PupilMenu() 
        strContinue = sys.stdin.readline() 
    else: 
        print "Login ID or password incorrect, press enter to continue" 
        strContinue = sys.stdin 
 
def loggedIn(user, password) 
    with open('studentsusers.csv', 'rb') as csvfile: 
        spamreader = csv.reader(csvfile, delimiter=',') 
 
        for row in spamreader: 
            strLoginID, strLoginPasswd = row 
 
            while strLoginID != "": 
                if strLoginID == user and strLoginPassWd == password: 
                    print user, "Logged In" 
                    return True 
                else: 
                    break