Home > database >  not exit key in dictionary
not exit key in dictionary

Time:10-02

I have a dictionary and when I want to use it in my code I cant find any key with given key. but key is exist my code. also it save duplicate key in my dictionary because it not recognize my key in dictionary

profile_account={"3286686": {"password": "hjhljjlllllllllll", "username": "oubbbbb", "trader_code":
0, "bot_code": 0, "addres_wallet": "", "state_user": "-", "expire_Data": "", 
"telegram_username": "future0bot", "Register": true, "trader": false, "is_bot": false}, 
"3286686": {"password": "ryyyyyyyyyy", "username": "urmuyr", "trader_code": 0, 
"bot_code": 0, "addres_wallet": "", "state_user": "-", "expire_Data": "", 
"telegram_username": "future0bot", "Register": true, "trader": false, "is_bot": false}}

also when I print them in my code I get this result

print(profile_account)
print(message.from_id,[*profile_account])
print(message.from_id not in profile_account)

output:

{'328660186': {'password': 'ryyyyyyyyyy', 'username': 'urmuyr', 'trader_code': 0,
'bot_code': 0, 'addres_wallet': '', 'state_user': '-', 'expire_Data': '', 
'telegram_username': 'future0bot', 'Register': True, 'trader': False, 'is_bot': False}}
328660186 ['328660186']
True

also I dump and load this dictionary with below method

json.dump(profile_account, open('profile_account.json', 'w'))
profile_account = json.load(open('profile_account.json'))

CodePudding user response:

it's because the message.from_id variable is an integer variable type and your dictionary keys are the string.
you have to convert them to a same type, like :

from_id = str(message.from_id)
print(profile_account[from_id])
  • Related