问题描述
我一直在尝试制作某种登录系统,所以现在,我有了它,以便如果用户名和密码在test.txt(有多个)中,它应该可以让您登录,我什至没有通过了验证用户名和密码是否在txt文件中并破坏了我的步骤,我不知道该怎么做,我尝试了好几个小时,然后尝试这样做,以便“如果您在文本文件中找到此用户名,给我行号,并检查此密码的密码是否与用户名相同,(我使用split(',')),如果输入的电子邮件和密码都在txt文件中并且在同一行中,则..(没有这样做)。
所以这让我很困惑,很多错误,如果没有错误,那么它就不会按照我的预期工作,这是我的意大利面条代码
def test():
with open('test.txt','r') as f:
for num,line in enumerate(f,1):
username = line.split(',')
if username in num:
if username == q1:
print("found user in line: " + num)
Line = num
password = line.split(',')
if password in Line:
if password == q2:
print("found pass in line: " + num)
有人可以帮助我解决此问题,并向我解释如何将数据存储在.txt文件中以及如何检索它们吗? youtube和google并没有太大帮助,如果您可以建议一个也很酷的视频,因为此时我很困惑,我只剩下尝试试用mongoose.db,因为它具有检索数据和存储已经内置的功能。 ,请帮助我
解决方法
如果要使用文本文件,请举一个简单的示例:
cat test.txt
aklaver,dog
adrian,cat
alklaver,fish
user_name = 'aklaver'
with open('test.txt','r') as pwd_file:
lines = pwd_file.readlines()
for line in lines:
user,pwd = line.split(',')
if user == user_name:
print(pwd)
dog
,
使用json,您可以通过以下方式完成此操作:
JSON文件
{
"user1":{"password":"123456"},"user2":{"password": "abcde"}
}
Python
import json
def test(username,password):
with open("answer.json","r") as read_it:
data = json.load(read_it)
if data[username][password] == '123456':
print('User found!')
else:
print('User or password doesn\'t exist')
test('user1','password')