Python 检查列表和组中是否有内容,并检查密码输入是否在其下一行

Python 检查列表和组中是否有内容,并检查密码输入是否在其下一行,python,python-3.x,python-3.4,Python,Python 3.x,Python 3.4,我希望它从这个文件读取,以检查输入的用户名和密码是否存在于文本文件列表中,但我需要在找到用户名的那一行之后的下一行检查密码 file = open("usernamepasswordfile2.txt", "r") lines = file.readlines() username1 = input("Please enter your username: ") password1 = input("Enter password: ") if username1 in lines[0] or

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

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
readlines()
保留行尾,使用例如
str.strip()
删除它们。

只需更改

lines = file.readlines()


由于
readlines()
在每个条目上都有“\n”

我怀疑
readlines
保留了每行末尾的换行符
\n
。因此,如果您的用户名是“苹果”
,则它不在列表中 您可以通过使用从每个读入行中剥离空白来修复此问题

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

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
  • 然后检查数据(使用for循环中的range函数,可以设置序列中每个数字之间的开始-停止和差值。)

创建一个字典(read)怎么样,其中键是用户名,值是密码?基本上,一个条目映射
username-->password
?听起来不错?好的,我们是这样做的:

您的
用户名密码文件2.txt
密码文件似乎具有以下结构:

奇数(1,3,5…)行包含用户名,偶数(2,4,6)行包含密码,对吗?(密码行在其正上方的行中包含用户的密码)

您可以利用
.readline()
将文件的偏移量提前到下一行的开头这一事实,这样您就可以在文件上组合迭代(
对于f
中的行,如果您不执行任何操作…呃…特殊…在文件内容上为您提供一个逐行迭代器)在循环中使用手册
readline()
有效读取
中f
行的奇数行(也称用户名)和下面
密码=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")
好吧,这里发生了什么

当您打开一个文件并对my_file:
中的任何内容执行
操作时,您将循环文件的内容,并将其存储在包含该文件的行中。但是,当您在
for
循环中执行
readline
时,实际上只前进了一行。可以这样想:

第一次迭代:

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

第二次迭代:

for line in f
-->读取文件的第三行(因为
f.readline()
确实前进了一行,记得吗?)。这意味着
包含第二个用户名。 在
for
中,执行一个
f.readline()
-->,返回文件的第4行,意思是:您在
行中拥有的用户密码(文件中的第二个用户)

所以你有这样的东西:

for line in f:   # Lines 1   3   5   ...
    #                     \   \   \
    f.readline() # Lines   2   4   6 ...
根据需要使用泡沫、冲洗和重复(根据需要……好吧……直到您阅读完整个文件)

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


由于您可能是出于学习目的而使用它,因此我建议您放置大量的
print
语句,以便您能够准确地了解正在发生的事情

我唯一不喜欢的是部分匹配、空字符串或换行符可能出现误报
for line in f:   # Lines 1   3   5   ...
    #                     \   \   \
    f.readline() # Lines   2   4   6 ...