Python 在txt文件中搜索变量时,不会超过第一行
在文本文件中搜索用户号码时,它只查看第一行,而从不查看第二行。它查找行中的最后一个变量,因为这是我在另一个文件中使用的变量。 如果第8行是“while”而不是“if”,我也会遇到这个错误 谢谢你的帮助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
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循环会导致错误,主要是因为它无限次地从第一行开始询问计数
字节。