Jun-17-2024, 11:42 AM
(This post was last modified: Jun-17-2024, 11:42 AM by Pedroski55.)
They should have written usernames, that would be less confusing for you perhaps.
Try like this maybe? Make your own file, then save it and then open it.
Of course, you need to set your own path, don't try to use my path!
Try like this maybe? Make your own file, then save it and then open it.
Of course, you need to set your own path, don't try to use my path!
import json from pathlib import Path path2json = '/home/pedro/temp/usernames.json' mypath = Path(path2json) data = [] username = 'X' # enter usernames and emails while not username == 'q': users_dict = {} username = input(f'Enter your username, enter q to quit ... ') if username == 'q': break email = input('enter your email ... ') users_dict['user name'] = username users_dict['user email'] = email data.append(users_dict) # save the data with open(mypath, 'w', encoding='utf-8') as fp: json.dump(data, fp, ensure_ascii=False, indent=4) # open the data to inspect it with open(mypath) as fp: data_list = json.load(fp) # find Tarzan for d in data_list: if d['user name'] == 'Tarzan': print(d) print(d['user name'])
Output:{'user name': 'Tarzan', 'user email': '[email protected]'}
Tarzan