Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 在txt文件中搜索变量时,不会超过第一行_Python_Python 3.x - Fatal编程技术网

Python 在txt文件中搜索变量时,不会超过第一行

Python 在txt文件中搜索变量时,不会超过第一行,python,python-3.x,Python,Python 3.x,在文本文件中搜索用户号码时,它只查看第一行,而从不查看第二行。它查找行中的最后一个变量,因为这是我在另一个文件中使用的变量。 如果第8行是“while”而不是“if”,我也会遇到这个错误 谢谢你的帮助 count =0 user_text = str(input("Enter user num")) found = False def Check(user_text): global count, found #while looking for line

在文本文件中搜索用户号码时,它只查看第一行,而从不查看第二行。它查找行中的最后一个变量,因为这是我在另一个文件中使用的变量。 如果第8行是“while”而不是“if”,我也会遇到这个错误

谢谢你的帮助

count =0
user_text = str(input("Enter user num"))
found = False
def Check(user_text):
    global count, found
    #while looking for line
    fh =open("user_info.txt", "r")
    if found == False:
        print("Here")
        s =fh.readline(count)
        print(s)
        #seperate the words
        N,M,A,B=s.split("~")
        #if its found
        if user_text in B:
            found = True
            print ("line Number:", count, ":", s)
        count+=1
        print(found)
    fh.close()
    return found

found = Check(user_text)

您正在使用
fh.readline()
。这将从文件中返回一行,您可以将其与可选参数一起使用,该参数显示要返回的行的字节数。如果要迭代这些行,请使用
fh.readlines()
(返回行列表)。

此处没有循环或任何内容。你只看了一行,我看不到循环!我想你需要一个
while
loop();e、 g.
while found==false
:Matthew已经指出while循环会导致错误,主要是因为它无限次地从第一行开始询问
计数
字节。