简体   繁体   English

Python 读取 JSON 文件字典

[英]Python read JSON file dictionary

Edit: Thnx for reply's everyone.编辑:谢谢大家的回复。 I guess I was programming blind!我想我是在盲目地编程!

I was wondering if anyone could help me with this code.我想知道是否有人可以帮助我处理这段代码。 I'm trying to read in a JSON file and check if the E-mail and password match the data requested.I've tried various things for a few hours and every time I get new errors, that's why I decided to ask the question online otherwise I'll be tinkering for hours more.我正在尝试读取JSON 文件并检查电子邮件和密码是否与请求的数据匹配。我已经尝试了几个小时的各种事情,每次我遇到新的错误,这就是为什么我决定问这个问题在线,否则我会再修修补补几个小时。

JSON FILE users.json: [{"email": "Mark", "password": "Hi123", "role": "Owner"},{"email": "Elsje", "Password": "Hi123", "Role": "Family member"},{"email": "Fred", "Password": "Hi123", "Role": "Owner"}] JSON FILE users.json: [{"email": "Mark", "password": "Hi123", "role": "Owner"},{"email": "Elsje", "Password": "Hi123", “角色”:“家庭成员”},{“电子邮件”:“Fred”,“密码”:“Hi123”,“角色”:“所有者”}]

import json
import time


count = 0  # Count the number of failed login attempts starting with 0.
current_time = time.localtime()  # Add the current time.
clock = time.strftime("%I:%M:%S %p", current_time)


def open_file(filename): # Function to open files and read them.
    with open(filename) as json_file:
        test = json.load(json_file)
        return test
    

read_file = open_file("users.json")
print(read_file)
print(type(read_file))

while True:
    email = input("\nEnter your username: ")
    passwords = input("Enter your password: ")
    count += 1  # Count the number of failed login attempts.
    login = False
    print("")

    **for item in read_file:
        if item[read_file]["email"] == email and item[read_file]["password"] == passwords:**
            print("Welcome", read_file[read_file]["email"], read_file[read_file]["role"], "you successfully logged in at", clock, "\n")
            count = 0
            login = True

    if login: break
    if not login:
        print("Incorrect E-mail or password!\n")
    if count > 5:  # I
        print("On", clock, "You have logged incorrectly 5 times. You are blocked for 15 minutes!")
        time.sleep(900)
        break

I'm getting the following error: line 28, in if item[read_file]["email"] == email and item[read_file]["Password"] == passwords: TypeError: unhashable type: 'list'我收到以下错误:第 28 行,在 if item[read_file]["email"] == email 和 item[read_file]["Password"] == passwords: TypeError: unhashable type: 'list'

Here is the working code edited by the comment suggested by @Samwise这是由@Samwise 建议的评论编辑的工作代码

import json
import time


count = 0  # Count the number of failed login attempts starting with 0.
current_time = time.localtime()  # Add the current time.
clock = time.strftime("%I:%M:%S %p", current_time)


def open_file(filename):  
    with open(filename) as f:
        return json.load(f)


read_file = open_file("2.json")
print(read_file)
print(type(read_file))

while True:
    email = input("\nEnter your username: ")
    passwords = input("Enter your password: ")
    count += 1  # Count the number of failed login attempts.
    login = False
    print("")

    for item in read_file:
        if item["email"] == email and item["password"] == passwords:

            print("Welcome", item["email"], item["role"], "you successfully logged in at", clock, "\n")
            count = 0
            login = True

    if login:
        break
    print("Incorrect E-mail or password!\n")
    if count > 5:  # I
        print("On", clock, "You have logged incorrectly 5 times. You are blocked for 15 minutes!")
        time.sleep(900)
        break

Just a little less confusion.只是少了一点混乱。

import json
import time

count = 0  # Count the number of failed login attempts starting with 0.
current_time = time.localtime()  # Add the current time.
clock = time.strftime("%I:%M:%S %p", time.localtime())

login = False

while not login:
    email = input("\nEnter your username: ")
    passwords = input("Enter your password: ")
    count += 1  # Count the number of failed login attempts.

    with open('users.json') as f:
        test = json.load(f)
        for item in test:
            if item["email"] == email and item["password"] == passwords:
                print("Welcome", item["email"], item["role"], "you successfully logged in at", clock, "\n")
                count = 0
                login = True
                break

    if not login:
        print("Incorrect E-mail or password!\n")
        if count > 2:  # I
            print("On", clock, "You have logged incorrectly 5 times. You are blocked for 15 minutes!")
            time.sleep(5)
            break

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

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