简体   繁体   中英

Python : Referencing Data from JSON file in python

I'm currently making a username + password login system and have a file containing the usernames and passwords of previously signed up users. The side of the programme that signs up users works perfectly, but I have tried to implement a system to check the data from the file and sign the user in if they input the right data.

Edit: One combination from the database, it's all structured like this; [["ExampleUsername", "BadPasswrdo14130"]]

This is the code:

# Login Interface using files as a 'login database' of sorts?

import json

def autoSave():
    with open("Accounts.json", "w") as outfile:
        json.dump(accounts, outfile)


def loadUsers():
    with open("Accounts.json") as infile:
        return json.load(infile)

def existingUser():
    eUsername = input("Your Username » ")
    ePassword = input("Your Password » ")

    for index, item in (accounts):
            if item[1] == eUsername and item[2] == ePassword:
                print("Logged in")
            else:
                print("Login failed")

def createUser():
    global accounts
    nUsername = input("Create Username »  ")
    nPassword =  input("Create Password »  ")
    entry = [nUsername, nPassword]
    accounts.append(entry)
    accounts = accounts[:500000]
    autoSave()

accounts = loadUsers()

How would I retrieve data from the database file and check that data with the data that the user inputted? I've tried already but it does not work, always says login failed, which is this bit of the code:

def existingUser():
eUsername = input("Your Username » ")
ePassword = input("Your Password » ")

for index, item in (accounts):
        if item[1] == eUsername and item[2] == ePassword:
            print("Logged in")
        else:
            print("Login failed")

In your code accounts is a list. When you're iterating over it, you get sublists . You should iterate over the sublists to get the username and password.

So you can do:

def existingUser():
    eUsername = input("Your Username » ")
    ePassword = input("Your Password » ")
    for item in accounts: #no need for braces
        if item[0] == eUsername and item[1] == ePassword:
            return "Logged in"
        else:
            return "Login failed"

print existingUser()

In your current code you seem to have forgotten that in Python list indexes start with 0 .

@ForceBru

This is the code I tried to prevent dup accounts, it doesent allow you to dupe accounts but when making a new account, it says its already created when its not, but creates it anyway.

def createUser():
global accounts
nUsername = input("Create Username »  ")
nPassword =  input("Create Password »  ")
for item in accounts:
    if item[0] == nUsername:
        return "Account Username already exists, try again"
    else:
        entry = [nUsername, nPassword]
        accounts.append(entry)
        accounts = accounts[:500000]
        autoSave()

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