【问题标题】:Find password by username通过用户名查找密码
【发布时间】:2015-12-23 03:28:36
【问题描述】:

我已经有了这个代码。现在我想更改代码,当有人输入他的用户名时,他必须填写属于该用户名的正确密码。

import csv
        csvbestand='inlog.csv'
        csv = open('inlog.csv', 'r').read().split('\n')[1].split(';')
        username= input("Fill in your username: ")
        if username == "admin" or username in csv:
            print("Username found")
            break
        else:
            print("Username not found")

    while True:
        import csv
        csvbestand='inlog.csv'
        csv = open('inlog.csv', 'r').read().split('\n')[2].split(';')
        password = input("Fill in your password: ")
        if password == "admin" or password in csv:
            print("Congratiulations")
            break
        else:
            print("Password not right")

所以当用户名是“John”时,我只想要属于“John”的密码作为正确的密码。

【问题讨论】:

  • 您至少需要向我们展示 csv 文件并解释您的代码到底有什么问题。 (说“它不工作”是不够的。)
  • 你的问题不清楚。

标签: python csv


【解决方案1】:

我假设你的 csv 会是这样的:

user1, hash_pass_u1
user2, hash_pass_u2
user3, hash_pass_u3
...

解决方案前只有一个注释。您正在导入 Python 的 CSV 模块,而您没有在代码中使用它,如此愚蠢的导入,只需使用它。

解决方法很简单

import csv

file = 'yourcsv.csv'
found = False
username = input('Write your username: ')
password_csv = None
with open(file, newline='') as csvfile:
   reader = csv.reader(csvfile, delimiter=',')
   for row in reader:
      # row[0] is the first element, the username and row[1] the hash of the password
      if row[0] == username:
         password_csv = row[1]
         found = True
         break
if not found:
   print('The username is not in our DB.')

while True:
   passw = input('Let me your password: ')
   hash_passw = your_method_to_get_the_hash(passw)
   if hash_passw == password_csv:
      print('Congrats, you are logged.')
      break
   else:
      print('Wrong password dude, try again.') 

这样,您只需读取一次文件,即可使用 CSV 模块。

我假设您的 CSV 格式如果是另一种格式,则很容易更改此解决方案的实施。如果您在 CSV 模块方面需要一些帮助,文档在此处,适用于 python2python3

解释你做错了什么。

当你做下面这句话时:

csv = open('inlog.csv', 'r').read().split('\n')[1].split(';')

您正在打开文件,读取所有文件,然后按\n 字符拆分文件,这样您将获得以下列表['user1;pass1';'user2;pass2','user3;pass3',...],最后一步是选择带有@987654328 的第二个元素@,结果将是字符串'user2;pass2'。但是这里的声明并没有结束,还有另一个拆分可以为您提供['user2','pass2'] 列表。

因此,您正在比较用户名是 admin 还是列表中的 ['user2','pass2']。当您尝试比较密码时也会发生同样的情况,但这次您选择了第三个元素。

【讨论】:

  • 好的!!谢谢!有效!非常感谢您的帮助!
【解决方案2】:
with open('Usernames.txt', 'r') as f:
   content = f.readlines()
   index = [x for x in range(len(content)) if password in content[x].lower()]
       index = (str(index)[1:-1])
       if index == '':
            print("user not found")
       else:
            index = (int(index))
            with open('passwords.txt', 'r') as d:
               d = d.readlines()
               f = (d[index]).strip()
               if password == f:
                    print("found password")

如果我想使用单独的文件(txt)来包含用户名和密码,我会这样做。它抓住了行号。用户名打开,然后并行输入密码

【讨论】:

    猜你喜欢
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 2018-05-15
    • 2018-07-27
    • 1970-01-01
    • 2022-07-18
    相关资源
    最近更新 更多