Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何知道何时到达文本文件的底部_Python_Text Files - Fatal编程技术网

Python 如何知道何时到达文本文件的底部

Python 如何知道何时到达文本文件的底部,python,text-files,Python,Text Files,因此,我有一个文本文件,其中包含程序用户的详细信息 在我的程序中,我有一个登录系统,可以从这个文本文件中检索数据,但我不知道如何判断何时到达它的末尾 文本文件中的数据示例如下所示: username = input("Enter username: ") password = input("Enter password: ") file = open("userDetails.txt", "r") match = False while match == False: finished

因此,我有一个文本文件,其中包含程序用户的详细信息

在我的程序中,我有一个登录系统,可以从这个文本文件中检索数据,但我不知道如何判断何时到达它的末尾

文本文件中的数据示例如下所示:

username = input("Enter username: ")
password = input("Enter password: ")

file = open("userDetails.txt", "r")
match = False
while match == False:
    finished = False
    while finished == False:
        for each in file:
            each = each.split(", ")
            realUser = each[1]
            realPass = each[2]
            if realUser == username and realPass == password:
                match = True
                finished = True
                print("match")
            elif each == "":
                finished = False
                username = input("Re-enter username: ")
                password = input("Re-enter password: ")
                match = False
            else:
                finished = False
鲍勃,15岁,足球,15岁

我的代码如下所示:

username = input("Enter username: ")
password = input("Enter password: ")

file = open("userDetails.txt", "r")
match = False
while match == False:
    finished = False
    while finished == False:
        for each in file:
            each = each.split(", ")
            realUser = each[1]
            realPass = each[2]
            if realUser == username and realPass == password:
                match = True
                finished = True
                print("match")
            elif each == "":
                finished = False
                username = input("Re-enter username: ")
                password = input("Re-enter password: ")
                match = False
            else:
                finished = False
我不确定的部分是elif each==:部分


有什么想法吗?

当循环结束到达文件末尾时,正确的代码是:

file = open("userDetails.txt", "r")
match = False
while match == False:
    for each in file:
        each = each.split(", ")
        realUser = each[1]
        realPass = each[2]
        if realUser == username and realPass == password:
            match = True
            print("match")
        elif each == "":
            username = input("Re-enter username: ")
            password = input("Re-enter password: ")
            match = False

考虑到这个问题,我认为在这里使用子句是理想的。 注意:for循环的else子句在循环正常退出时执行,即没有遇到break语句

算法 1.请求用户输入 2.最初将标志匹配设置为False 3.在文件中搜索,直到找到匹配项。 4.使用上下文管理器打开文件;确保操作后关闭文件。 5.找到匹配项后,立即中断文件循环,同时将标志match设置为True。 6.如果未找到匹配项,请要求用户重新输入凭据并再次执行搜索操作。 7.继续,直到找到匹配项

代码


提示:您还可以让CSV模块尝试用Python读取数据文件。

在for-each-in-file:loop之后,文件指针位于文件的末尾。不需要从循环中进行检查。@schwobaseggg我不太明白你说的是什么意思循环在文件中的所有行中循环。循环完成后,就不再有行了。