skip to Main Content

When ever I try to load data from a json file I get this error:
AttributeError: ‘list’ object has no attribute ‘load’

What I expected

import json
users=[]
with open('username project.json','w') as f:
    jfile=json.dump(users,f)

def create_usernames():
    username=input('Enter in username')
    with open('username project.json','r+') as read:
        usersj=users.load(read)
    if username in users[usersj]:
            print('username rejected')
    else:
        print('username is okay')
create_usernames()

What resulted

usersj=users.load(read)
AttributeError: 'list' object has no attribute 'load'

2

Answers


  1. import json
    users=[]
    with open('username project.json','w') as f:
        jfile=json.dump(users,f)
    
    def create_usernames():
        username=input('Enter in username')
        with open('username project.json','r+') as users: #changed this to users
           usersj=users.load(read)
        if username in users[usersj]:
                print('username rejected')
        else:
            print('username is okay')
    create_usernames()
    

    are you trying to load the json users? because list doesn’t have any load method

    Login or Signup to reply.
  2. There’re couple of typos in your code, and I corrected accordingly

    import json
    users=[]
    with open('username project.json','w') as f:
        jfile=json.dump(users,f)
    
    def create_usernames():
        username=input('Enter in username')
        with open('username project.json','r+') as read:
            usersj=json.load(read) # <-- updated this
        if username in usersj: # <-- and this
                print('username rejected')
        else:
            print('username is okay')
    create_usernames()
    

    Tested both the scenarios where a username exists and does not exist

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search