简体   繁体   中英

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. 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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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