检查列表和组中是否有内容,并检查密码输入是否为以下行

问题描述 投票:0回答:5

我希望它从这个文件中读取以检查输入的用户名和密码是否存在于文本文件列表中,但我需要在找到用户名的行后面的下一行检查密码。

file = open("usernamepasswordfile2.txt", "r")
lines = file.readlines()
username1 = input("Please enter your username: ")
password1 = input("Enter password: ")

if username1 in lines[0] or lines[0:900]:
    print("access granted")
else:
    print("access denied")

usernamepasswordfile2.txt的内容:

zkhan
3J1R7r
tsd
f9kYrD
zkay
JYyNdC
hdend
mYR2OC
fkhan
JeL6u5
python python-3.x python-3.4
5个回答
3
投票

如何创建一个字典(读取the docs),其中键是用户名,值是密码?基本上,条目username --> password的地图?听起来不错?好的,这是我们这样做的方式:

您的usernamepasswordfile2.txt密码文件似乎具有以下结构:

奇数(1,3,5 ......)行包含您的用户名,甚至(2,4,6)行包含密码,对吗? (密码行包含用户在其右上方的密码)

您可以利用.readline()将文件的偏移量推进到下一行的开头这一事实,因此您可以将迭代结合到文件上(for line in f,如果您不做任何事情......呃... special ...给你一个逐行迭代器在文件的内容上)用循环里面的手册readline()来有效地读取for line in f中的奇数行(aka用户名)和password = f.readline().strip()中的偶数行:

import pprint

users_passwords = dict()  # Better user_passwords = {}, but let's be explicit

f = open("usernamepasswordfile2.txt", "r")

for line in f:
    user = line.strip()
    print("This is the line I read: %s, this is the stripped version: %s" %
          (repr(line), line.strip()))
    password = f.readline().strip()
    print("Read user=%s, password=%s" % (user, password))
    users_passwords[user] = password

f.close()  # Yep, we have already loaded the contents
           # in the `user_passwords` dict, so let's 
           # close it (we're not gonna need it anymore)

print("Fetched:\n%s" % pprint.pformat(users_passwords))
print("The usernames are the users_passwords keys:\n%s" %
      pprint.pformat(users_passwords.keys()))
print("For instance, what is the password for username %s? %s"
      % ('zkhan', users_passwords['zkhan']))

username1 = input("Please enter your username: ")
if username1 in users_passwords.keys():
    print("access granted")
else:
    print("access denied")

好的,那么这里发生了什么?

当您打开文件并执行for whatever in my_file:时,您将遍历存储在whatever中的文件内容包含文件的行。但是,当您在readline循环中执行for时,您实际上是在前进一行。把它想象成:

第一次迭代:

for line in f - >读取文件f中的第一行(并准备阅读下一行)。好的,这是一个用户名(文件的第一行)。在那个for中,你做一个f.readline() - >读取(并推进一行)第二行。这是一个密码(您在第一行中拥有的密码,截至目前,该密码存储在line中)

第二次迭代:

for line in f - >读取文件的第3行(因为f.readline() DID前进一行,还记得吗?)。这意味着line包含第二个用户名。在for中,你做一个f.readline() - >返回文件的第4行,这意味着:你在line中拥有的用户的密码(文件中的第二个用户)

所以你有这样的事情:

for line in f:   # Lines 1   3   5   ...
    #                     \   \   \
    f.readline() # Lines   2   4   6 ...

根据需要进行泡沫,冲洗和重复(根据需要......好......直到您阅读完整个文件)

那是什么...... strip()的事情?好吧,当你从文件中读取一行时,每一行也会返回结束字符(换行符:\n)。但你不想要这个角色,对吧?由于换行符是空白字符,因此您可以获得行的剥离版本。

由于你可能是出于学习目的而使用它,我建议你放置很多print语句,这样你就可以完全按照正在发生的事情进行操作。


2
投票

只需改变

lines = file.readlines()

lines = file.read()

因为readlines()每个条目都有“\ n”


1
投票

我怀疑\n保留了每一行末尾的换行符readlines。因此,如果您的用户名是'apple',则不在列表['apple\n', 'boy\n', ...]中。 您可以通过从每个读入行中删除空白来解决此问题

with open("usernamepasswordfile2.txt", "r") as file:
    lines = [line.rstrip('\n') for line in file.readlines()]

1
投票

试试这个:

file = open("test.txt", "r")

lines = file.readlines()

username = "heyyy"
passw = "heyyyy"

for i in range(len(lines)):
   lines[i] = lines[i].strip('\n')

for i in range(0,len(lines),2):
   if username == lines[i]:
      if passw == lines[i+1]:
          print("acces")
  • 打开文件并从中获取所有数据
  • 然后你去掉所有的\ n \ n行
  • 然后检查数据(使用for循环中的范围功能,您可以设置开始停止和序列中每个数字之间的差异。)

1
投票

readlines()保留行结尾,例如使用str.strip()删除它们。

© www.soinside.com 2019 - 2024. All rights reserved.