Python从用户输入的内容中读取下一行

时间:2015-10-17 21:07:42

标签: python

所以基本上我有一个程序可以在文档中创建用户用户名和密码,并搜索与使用该程序的人输入的用户名一起使用的密码。

例如: 程序要求我输入我输入的用户名:'13'。 文本文档中的13下是'sadfsdfsdf'。 我希望程序跳到'Username:13'以下,然后阅读并打印'Password:sadfsdfsdf'。

请注意,我在.txt文件中有多个用户名和密码

u ='用户名:'

提前致谢!

def SearchRecordProgram():
    while True:
                    user_search = input("Please enter the username you wish to see the password for: ")
                    full_user = u + user_search
                    if full_user in open('User_Details.txt').read():
                            print(" ")
                            print ("Username found in our records")
                            print(" ")
                            print(full_user)
                            break
                    else:
                            print("Username entered does not match our records")

4 个答案:

答案 0 :(得分:1)

所以,想象你的文件是这样的:

Username : 13
Password : sadfsdfsdf
Username : 15
Password : Lalalala

您可以解析它(使用正则表达式),如下所示:

import re  #  regular expression module
regex = re.compile("Username :(.*)\nPassword :(.*)")

# read text from file
with open(filePath,'r') as f:
    text = f.read() 

u_pass = regex.findall(text)
# [(' 13', ' sadfsdfsdf'), (' 15', ' Lalalala')]

user_dict = {u:password for u,password in u_pass}
#  {' 13': ' sadfsdfsdf', ' 15': ' Lalalala'}

现在,您可以通过询问该用户的密码来获取某人的密码:

# given username and password_input

if username in user_dict and user_dict[username] == password_input:
# if username exists and password okay
    print "Authentication succeeded."

答案 1 :(得分:0)

也许不是美女,但这样的事情会奏效:

with open(users_file, "r") as f:
    lines = f.read()

def get_password(user_id):
    entries = iter(lines.splitlines())
    for entry in entries:
        if(entry.startswith("{}:{}".format(prefix, user_id))):
            return next(entries)

print "Password:", get_password("13")

答案 2 :(得分:0)

def SearchRecordProgram():
    while True:
        user_search = input("Please enter the username > ")

        file = open('User_Details.txt')
        usernames = file.readlines()  # Read file into list
        file.close()

        if user_search in usernames: # Username exists
            print ('Found Username')
            password = usernames[usernames.index(user_search)+1]
            print ('Password is: ' + password)   

            break  # Exit loop

         else:  # Username doesn't exist
             print("Username entered does not match our records")

请注意,如果密码恰好是用户名,则无法使用,例如:

user1
password1
user2
user3  # This is the password for user3
user3
password3

如果您搜索“user3”,此代码将输出“user3”作为“user3”的密码,因为它找到“user3”的第一个实例(第4行),然后查看下一行(第5行),这是下一个用户名。

更糟糕的是,如果“user3”是文件中的最后一行,它将以错误结束,因为没有更多行。您可以使用以下代码添加检查找到的用户名的索引是否为偶数(即索引0,2,4,8):

if not usernames.index(user_search) % 2:  # Checks the remainder after / by 2.
                                          # If it's 0 then it's an even index.
    password = usernames[usernames.index(user_search)+1]
    print ('Password is: ' + password)   

但如果发生这种情况,你就无能为力。

但是,您可以将用户名文件操作为仅具有以下用户名:

lst = [0, 1, 2, 3, 4, 5, 6, 7]
print (lst[0::2])

打印

[0, 2, 4, 6]

所以代码可以改为:

def SearchRecordProgram():
    while True:
        user_search = input("Please enter the username > ")

        usernames = open('User_Details.txt').readlines()  # Read file into list

        if user_search in usernames[0::2]: # Username exists
            print ('Found Username')
            password = usernames[1::2][usernames[0::2]].index(user_search)]
            print ('Password is: ' + password)   

            break  # Exit loop

         else:  # Username doesn't exist
             print("Username entered does not match our records")

这是一个细分

password = usernames[1::2]  # Gets the odd items (passwords)
                          [  # Get index item. The index returned from searching
                             # the usernames will be the same as the index needed
                             # for the password, as looking at just the odd items:
                             # [0, 2, 4, 6]
                             # [1, 3, 5, 7] - even has the same index as next odd.
                           usernames[0::2] # Gets even items (usernames)
                                          ].index(user_search)]
                                          # Only searches the usernames. This
                                          # means passwords being the same is not
                                          # an issue - it doesn't search them

答案 3 :(得分:0)

如果要打开文件,几乎应该始终使用with语句:

with open('User_Details.txt') as read_file:
    # Do reading etc. with `read_file` variable

这将确保正确处理任何错误并且文件未保持打开状态。

现在该文件已打开,我们需要遍历每一行,直到找到与我们的用户名匹配的行。我希望你知道for loop是如何运作的:

username = 'Username: 13'  # Get this however you want
with open('User_Details.txt') as read_file:
    for line in read_file:
        line = line.strip()  # Removes any unnecessary whitespace characters
        if line == username:
            # We found the user! Next line is password

我们需要获取包含密码的下一行。有很多方法可以获得下一行,但一种简单的方法是使用next()函数,它只是从迭代中获取下一个元素(在这种情况下是文件的下一行):

username = 'Username: 13'
with open('User_Details.txt') as read_file:
    for line in read_file:
        line = line.strip()
        if line == username:
            password = next(read_file)
            break  # Ends the for loop, no need to go through more lines

现在您有了密码和用户名,您可以随心所欲地使用它们。将输入和输出置于程序逻辑之外通常是一个好主意,所以不要在for循环内打印密码,而是在那里接收密码然后在外面打印。

您甚至可能希望将整个搜索逻辑转换为函数:

def find_next_line(file_handle, line):
    """Finds the next line from a file."""
    for l in file_handle:
        l = l.strip()
        if l == line:
            return next(file_handle)


def main():
    username = input("Please enter the username you wish to see the password for: ")
    username = 'Username: ' + username
    with open('User_Details.txt') as read_file:
        password = find_next_line(read_file, username)
    password = password[len('Password: '):]
    print("Password '{0}' found for username '{1}'".format(password, username))

if __name__ == '__main__':
    main()

最后,以这种格式存储任何是绝对疯狂的(更不用说密码安全的东西了,但我让你只是学习东西),为什么不做点什么像:

username:password
markus:MarkusIsHappy123
BOB:BOB'S PASSWORD

然后可以很容易地将其转换为词典:

with open('User_Details.txt') as read_file:
    user_details = dict(line.strip().split(':') for line in read_file)

现在要获取用户名的密码,您可以:

username = input('Username: ')
if username in user_details:
    print('Password:', user_details[username])
else:
    print('Unknown user')