简体   繁体   English

如何从 python 脚本的文本文件中 select 某些行?

[英]How do I select certain lines in a text file from python script?

So I'm making a python script where you can create an account and that account is saved in a text file.所以我正在制作一个 python 脚本,您可以在其中创建一个帐户并将该帐户保存在一个文本文件中。 When you try to log in, it will look in the text file for your username and then move down a line for the password but I don't know how to move down a line after finding the username.当您尝试登录时,它会在文本文件中查找您的用户名,然后下移一行输入密码,但我不知道在找到用户名后如何下移一行。 Any help would be appreciated.任何帮助,将不胜感激。 :) :)

Update -更新 -

import time
import sys


print ("Do you have an account?")
account = input()
if account == "Yes":
    print ("Enter your username")
    enterUsername = input()
    with open ("Allusers.txt") as f:
        if enterUsername in f.read():
            print ("Enter your password")
            enterpassword = input()
            if enterpassword in f.read():
                print ("Logged in")
            if enterpassword not in f.read():
                print ("Wrong password")
if account == "No":
    print ("Create a username") 
    createUsername = input()
    with open ("Allusers.txt") as f:
        if createUsername in f.read():
            print ("Username already taken")
            sys.exit()
        if createUsername not in f.read():
            print ("Create a password")
            createPassword = input()
            with open ("Allusers.txt") as f:
                if createPassword in f.read():
                    print ("Password not available")
                    sys.exit()
                if createPassword not in f.read():
                    file_object = open ('Allusers.txt', 'a')
                    file_object.write("" + createUsername + "\n")
                    file_object.close()
                    file_object = open ('Allusers.txt', 'a')
                    file_object.write("" + createPassword + "\n")
                    file_object.close()
                    print ("Done")
            

This is still work in progress and most likely still has errors here and there.这仍在进行中,很可能仍然存在错误。

Assumin that your file look like this:假设您的文件如下所示:

Adam
password
John
12345678
Horacy
abcdefg
Romek
pass1234

You can try this example:你可以试试这个例子:

user = "Horacy"
password = "abcdefg"

with open( "users.txt", "r" ) as file:
    for line in file:
        if user == line.strip():
            if password ==  file.readline().strip():
                print( "Correct" )
                break 
def get_password(file, username): lines = open(file, "r").readlines() # get the lines from the file for i, line in enumerate(lines): if line == username: # if the current is the username, return the following line return lines[i + 1]

As stated if someones password equals someones username iterating over all lines and checking may return faulty results you'll want to check only usernames as you iterate, so zipping every other line you can check the username only and return the password: 如前所述,如果某人的密码等于某人的用户名,遍历所有行并且检查可能会返回错误的结果,您将只想在迭代时检查用户名,因此压缩每隔一行,您可以仅检查用户名并返回密码:

def get_password(file, username):
    with open(file, "r") as f:
        data = f.readlines()
        for user, pw in zip(data[::2], data[1::2]):
            if user.strip() == username:
                return pw.strip()

You should only search in usernames.您应该只搜索用户名。 The data[::2] will select usernames. data[::2]将 select 用户名。

with open("filename", "r") as f:
        data = f.read().splitlines()
        email = "email@email"
        if email in data[::2]:
                id_email=data[::2].index(email)
                row=id_email*2-1
                password=data[row+1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM