Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Text Files - Fatal编程技术网

使用Python搜索文本文件中的列表

使用Python搜索文本文件中的列表,python,list,text-files,Python,List,Text Files,我有一个注册帐户的程序,我现在正在开发一个函数来登录这个帐户。我已将“用户名”和“密码”列表保存到单独的文本文件中 usernames = [] passwords = [] usernames.append(username) passwords.append(password) usernamesFile = open("usernames.txt","a") for usernames in usernames: usernamesFile.write("%s\n" % user

我有一个注册帐户的程序,我现在正在开发一个函数来登录这个帐户。我已将“用户名”和“密码”列表保存到单独的文本文件中

usernames = []
passwords = []
usernames.append(username)
passwords.append(password)

usernamesFile = open("usernames.txt","a")
for usernames in usernames:
    usernamesFile.write("%s\n" % usernames)
usernamesFile.close()

passwordsFile = open("passwords.txt", "a")
for passwords in passwords:
    passwordsFile.write("%s\n" % passwords)
passwordsFile.close()

while True:
    loginUsername = raw_input("Enter your username: ")
    usernamesFile = open("usernames.txt", "r")
    lines = usernamesFile.readlines() 
    if loginUsername in usernames:
        index = usernames.index(username)
        break
    else:
        print "Username not found"
        continue

我的问题是,即使我测试了文本文件,它保存了我注册的所有用户名,搜索结果仍然是“用户名未找到”。我不是Python方面的专家,所以如果你能用一种简单的方式解释一下,那就太好了

这是因为你是这样写的

usernamesFile.write("%s\n" % usernames)
“\n”
添加到每个用户名的末尾,以便

if loginUsername in usernames:

始终为
False
,除非您删除
“\n”
或使用
loginUsername
添加它,否则您没有在代码中使用
读取
用户名.txt
文件来搜索用户名。因此,将代码修改为:

usernames = []
passwords = []
usernames.append(username)
passwords.append(password)

usernamesFile = open("usernames.txt","a")
for usernames in usernames:
    usernamesFile.write("%s\n" % usernames)
usernamesFile.close()

passwordsFile = open("passwords.txt", "a")
for passwords in passwords:
    passwordsFile.write("%s\n" % passwords)
passwordsFile.close()

while True:
    loginUsername = raw_input("Enter your username: ")
    usernamesFile = open("usernames.txt", "r")
    lines = usernamesFile.readlines() 
    if loginUsername in lines:
        index = lines.index(username)
        break
    else:
        print "Username not found"
        continue

看起来你的代码从来没有对
行做过任何事情,我猜这就是你想要搜索的内容?(另外:作为旁注:即使你只是在学习python,你也应该立即清除将密码列表存储在clear中的想法)。我如何不将密码列表存储在clear中?