Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

json - Python - Get Value from List in Dictionary

I am working on some software to use across multiple computers, and has an FTP server that will be created using the library pyftpdlib to use for downloading needed files from the "manager". While I am setting up the authorization for it, I ran across the issue of how to get a value from a list, in my param dictionary, that is filled from a file named config.json. Here is my code below for if the file does not exist:

if not os.path.exists("config.json"):
    name = socket.gethostname()
    ip = socket.gethostbyname(name)
    params['ip'] = []
    params['ftp_creds'] = []
    params['output_directory'] = []
    params['ip'].append({
        'address': str(ip)
    })
    params['ftp_creds'].append({
        'username': 'root',
        'password': '12345',
        'directory': 'ftp_files'
    })
    params['output_directory'].append({
        'output-directory': 'render_out'
    })
    with open("config.json","w") as file:
        json.dump(params, file)
    print("File config.json did not exist, wrote it. Please restart script.")
    sys.exit()

Down further in the script, I have a couple lines of code setting up the "authorizer" that are a work in progress.

ftp_authorizer = DummyAuthorizer()
ftp_authorizer.add_user(perm="elradfmwMT")

The perm="elradfmwMT" argument will not be the only argument, as I will be getting values from the dictionary to set up username, password, etc. Any ideas?

question from:https://stackoverflow.com/questions/65947538/python-get-value-from-list-in-dictionary

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are trying to get the value from params['ftp_creds] what you have done is appended a dictionary to the list. params['ftp_creds'][0] is a dictionary from which you can access the password via params['ftp_creds'][0]['password'].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...