来自 csv 而不是文本文件的用户名和密码

username and password from csv instead of text file

我有一个文本文件来读写用户名和密码。我希望它在 csv 上有 1. 排序图表 2. 文本文件中的任何单词都可以用作用户名和密码,即使它们在不同的行中,它们也可以互换使用,这是无意的。我希望它只使用正确行中的信息。 比如[apple, pear] [banana, orange]。如果苹果和梨或香蕉和橙子在一起,它应该只接受它。此代码工作正常,它只是来自空白文本文件的 运行。

import time
import sys

text1 = input("\n Write username ")
text2 = input("\n Write password ")

saveFile = open('usernames+passwords', 'a')
saveFile.write(text1 + '    ' + text2 + '\n')

saveFile = open('usernames+passwords', 'r')
saveFile.seek(0)
uap = saveFile.read()

saveFile.close()

max_attempts = 3
attempts = 0

while True:
    print("Username")
    username = input("")

    print("Password")
    password = input("")

    if username in uap and password in uap:
        print("Access Granted")
    else:
        attempts+=1
        if attempts >= max_attempts:
            print(f"reached max attempts of {attempts} ")
            sys.exit()
        print("Try Again (10 sec)")
        time.sleep(10)
        continue
    break

所有的问题都是因为你使用 read() 并且你得到的都是单个字符串,然后它检查 password, username 的完整字符串。

您应该使用 readline() 将每一行作为分隔的字符串 - 稍后您需要 for-loop 来分别检查每一行中的 passwordusername

# ... code ...

save_file = open('usernames+passwords', 'a')
save_file.write(text1 + '   ' + text2 + '\n')
save_file.close()

read_file = open('usernames+passwords', 'r')
all_lines = read_file.readlines()
read_file.close()

max_attempts = 3
attempts = 0

while True:
    print("Username")
    username = input("")

    print("Password")
    password = input("")
    
    found = False
    for line in all_lines:
        if username in line and password in line:
           found = True
           break  # no need to check other lines
        
    if found:
        print("Access Granted")
        break
    else:
        # ... code ...

但是如果您将 password 设为 username 并将 username 设为 password.

则可以访问

您必须拆分每一行以使用 [password, username] 获取列表,然后使用 indexex [0][1] 对其进行检查,并使用 == 而不是 in 以确保它不在某个更长的字符串中 - 即。 "abc" in "xabcy" 给出 True"abc" == "xabcy" 给出 False

# ... code ...

save_file = open('usernames+passwords', 'a')
save_file.write(text1 + '   ' + text2 + '\n')
save_file.close()

read_file = open('usernames+passwords', 'r')
all_lines = read_file.readlines()
all_lines = [line.strip().split('   ') for line in all_lines]  # split values in lines
read_file.close()

max_attempts = 3
attempts = 0

while True:
   print("Username")
   username = input("")

   print("Password")
   password = input("")
   
   found = False
   for line in all_lines:
       if username == line[0] and password == line[0]:  # use indexes `[0]`, `[1]`
          found = True
          break  # no need to check other lines
       
   if found:
       print("Access Granted")
       break
   else:
       # ... code ...

当然,这样您就几乎拥有了 csv 文件 - 但使用 ' ' 而不是 , 作为分隔符。但是为此使用 csv 模块会更安全。但我记得它不能使用多字符分隔符,所以你必须删除 usernames+passwords 并用新代码重新创建它 - 或者你必须手动将文件分隔符 ' ' 替换为 ,

import csv

# ... code ...

save_file = open('usernames+passwords', 'a')
cvs_writer = csv.writer(save_file)
cvs_writer.writerow( [text1 , text2] )
save_file.close()

read_file = open('usernames+passwords', 'r')
cvs_reader = csv.reader(read_file)
all_lines = list(cvs_reader)
read_file.close()

max_attempts = 3
attempts = 0

while True:
    print("Username")
    username = input("")

    print("Password")
    password = input("")
    
    found = False
    for line in all_lines:
        if username == line[0] and password == line[1]:
           found = True
           break
        
    if found:
        print("Access Granted")
        break
    else:
        # ... code ...